Firebase通知和数据

时间:2016-08-11 06:35:28

标签: android firebase-cloud-messaging

我正在使用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>

2 个答案:

答案 0 :(得分:6)

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

点击通知后,FCM sample启动MainActivity:

{
    "rows":[
        {
            "Price":"1",
            "pd":"266",
            "Id":"59098",
            "Level":"5"
        },
        {
            "Price":"1",
            "pd":"266",
            "Id":"59098",
            "Level":"5"
        },
        //and again and again
        {
            "Price":"2",
            "pd":"247",
            "Id":"59098",
            "Level":"5"
        }
    ]
}

答案 1 :(得分:4)

如果您的应用位于前台,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());
    }