首页 > 解决方案 > 为什么在 android 版本 10 中没有触发 onMessagedRecieved?

问题描述

当我收到 FCM 的通知时,我想开始一项活动。它在应用程序的前台运行良好,但是当我在 android 中移动我的活动时,我会在系统托盘中收到通知。

正如我所做的那样,当应用程序位于前台时会调用 onMessagereceived,但当应用程序位于后台时不会调用它。

    class MyFirebaseMessagingService : FirebaseMessagingService() {

    // [START receive_message]
    private var mRemoteMessage: Map<String, String>? = null

    override fun onMessageReceived(remoteMessage: RemoteMessage) {


        // Check if message contains a data payload.
        if (!remoteMessage.data.equals("")) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification()!!.getBody());
            remoteMessage.data.get("notification_data")?.let { sendNotification(it) };

            val broadcast = Intent();
            broadcast.setAction("OPEN_NEW_ACTIVITY")
            sendBroadcast(broadcast)
        }

    }

    private fun sendNotification(messageBody: String) {
        val intent = Intent(this, NotificationActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(
            this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT
        )

        val channelId = getString(R.string.default_notification_channel_id)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationLayoutExpanded = RemoteViews(packageName, R.layout.activity_notify)

        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(getString(R.string.fcm_message))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setFullScreenIntent(pendingIntent,true)
            .setCustomBigContentView(notificationLayoutExpanded)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
                as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(0, notificationBuilder.build())

 }

我的清单

     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.BIND_JOB_SERVICE"
        tools:ignore="ProtectedPermissions" />

    <uses-permission
        android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
        tools:ignore="ProtectedPermissions" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppThemeNoActionBar">
        <activity android:name=".LauncherActivity">

        </activity>
        <activity
            android:name=".NotificationActivity"
            android:launchMode="singleTop"
            android:showOnLockScreen="true"
            android:screenOrientation="sensorPortrait"
            android:theme="@style/AppThemeNoActionBar">
            <intent-filter>
                <!-- Note: these actions are notification actions -->
                <action android:name="VIDEO_CALLING" />
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="OPEN_NEW_ACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <!--        <service
            android:name=".AppFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        &lt;!&ndash;-->

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

   
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="false" />
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="false" />


        <receiver android:name=".FirebaseDataReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".MyJobIntentService"
            android:permission="android.permission.BIND_JOB_SERVICE" />


        <service android:name="com.google.android.gms.measurement.AppMeasurementService"
            android:enabled="true"
            android:exported="false"/>

        <receiver android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.gms.measurement.UPLOAD" />
            </intent-filter>
        </receiver>

        <receiver android:enabled="true" android:name=".BootUpReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>


    </application>

</manifest>

请建议在后台收到通知时我该怎么做才能启动活动。?

标签: androidandroid-activitypush-notificationbackgroundfirebase-cloud-messaging

解决方案


您在通知中发送了数据。通知仅处理应用程序的前台,而不是您在有效负载中发送数据的通知。并基于有效负载消息打开活动

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)       
    val data = remoteMessage.data
    if (remoteMessage.data.isNotEmpty()) {
       Log.d(TAG_FIREBASE, "Message data payload: ${remoteMessage.data}")

      //open activity
  }}

而且还取决于您测试此服务的设备

  1. 谷歌像素
  2. 三星
  3. 华为

推荐阅读