【问题标题】:Firebase Notification and DataFirebase 通知和数据
【发布时间】:2016-12-17 18:04:45
【问题描述】:

我正在使用 Firebase 云消息传递 API 创建应用程序...我能够从服务器向我的客户端应用程序发送通知和数据。 但问题是当应用程序打开时,通知没有在数据出现时触发(我的意思是我记录了它)这不是问题。但是当应用程序关闭时,会收到通知,当我单击通知时,活动已打开,而我看不到日志数据。我需要将数据更新到 TextView..

我的 MyFirebaseMessagingService :

public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        Log.e("FROM", "From: " + remoteMessage.getFrom());
        String data = remoteMessage.getData().get("message");
        Log.e("KOIII", "Notification Message Body: " + remoteMessage.getNotification().getBody());
       // showNotificationS(remoteMessage.getNotification().getBody(),2);
        if(data.equals("true")){
            Log.e("DATA", "SUCCESS");
           // Fragment1.getInstace().updateTheTextView(data, data, data);
        }
    }

我的清单:

<activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".myHome"
            android:label="@string/app_name">
        </activity>
<service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

【问题讨论】:

    标签: android firebase-cloud-messaging


    【解决方案1】:

    当您发送带有数据负载(通知和数据)的通知消息并且应用程序在后台时,您可以从由于用户点击通知而启动的额外意图中检索数据。

    来自FCM sample,它在点击通知时启动 MainActivity:

    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Log.d(TAG, "Key: " + key + " Value: " + value);
        }
    }
    

    【讨论】:

    • 这是否获取数据值??还是活动的通知值?
    • 额外的只是来自数据负载的键/值对。
    • @ Arthur Thompson 我尝试将其添加到我的活动中。但是没有看到日志。我应该把它放在片段中吗?因为我正在使用片段..
    • 当您点击通知时,意图是否有任何额外的记录?此外,请求必须包含数据有效负载,可能会添加您的请求(如果您不使用控制台)和您的代码以将数据有效负载值记录到问题中。
    【解决方案2】:

    如果您的应用在前台,FCM 将不会显示通知,因此您手动显示如下:-

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            // TODO(developer): Handle FCM messages here.
            // If the application is in the foreground handle both data and notification messages here.
            // Also if you intend on generating your own notifications as a result of a received FCM
            // message, here is where that should be initiated. See sendNotification method below.
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "From: " + remoteMessage.getData().get("message"));
    //        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getData().get("message"));
        }
        // [END receive_message]
    
        /**
         * Create and show a simple notification containing the received FCM message.
         *
         * @param messageBody FCM message body received.
         */
        private void sendNotification(String messageBody) {
            Intent intent = new Intent(this, MarketActivity.class);
    //        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Fred Notification")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    

    【讨论】:

    • 是的,我能够做到这一点.. 但我需要的是.. 如果应用程序在后台并且通知被触发.. 但我的 logcat 中没有看到日志,因为我正在打印数据..我希望你得到这个场景..
    • 是的..我明白了..如果两者都使用了数据和通知,我希望这个问题存在..如果只使用数据,那么消息将一直收到并可以从中创建通知。 .stackoverflow.com/a/37876727/3910281
    猜你喜欢
    • 1970-01-01
    • 2018-06-06
    • 2017-02-26
    • 1970-01-01
    • 2020-05-21
    • 2018-01-28
    • 2016-12-07
    • 1970-01-01
    • 2018-02-21
    相关资源
    最近更新 更多