有什么更好的方法来实现android FCM推送通知?

时间:2017-07-09 16:49:32

标签: android firebase android-intent notifications firebase-cloud-messaging

我在使用Android应用的FCM推送通知时遇到了一个场景。

考虑我的应用程序结构就像(注册和登录屏幕是启动画面的一部分,所以在下面的例子中只提到登录)

Login => A 
Login => A => B 
Login => A => B => E
Login => A => B => C 
Login => A => B => C => E
Login => A => B => C => D

现在我可以收到推送通知,该推送通知只能是C或B.

在FCM推送通知中,有两种情况可能发生 1,app正在运行 2.应用程序未运行

案例1。 应用程序在前台运行,我可以根据推送通知将用户重定向到C或B.当我按下后退按钮时,我之前的活动就到了前台。 如果我的应用程序位于“最近的应用程序”列表中,并且我点击通知,它会直接转到活动C或B.按下后退按钮会导致活动被破坏,但我无法将用户重定向到活动A.我实现了吗?

以下是我处理FCM通知的方式

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        dataPayload = remoteMessage.getData();

        Intent intent = null;
        if (dataPayload.has("detail")) {
            intent = new Intent(this, C.activity);
            intent.putExtra("details",dataPayload.getString("details"));
        } else {
            intent = new Intent(this, B.activity);
        }
        sendNotification(intent)
    }
}

private void sendNotification(Intent nwIntent) {
    //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, nwIntent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

案例2。 应用程序未在后台运行。在这种情况下,FCM通知处理意图并启动应用程序,但不会重定向到特定活动C或B. 所以这里首先出现登录屏幕,然后是活动A.

如何在不加载活动A的情况下将用户重定向到C或B活动?在活动A的生命周期方法中,我需要处理Intents,因此活动A不会对web api进行任何数据调用,而是直接重定向到活动C或B,当我按下后退按钮时,它应该重定向到A ?

0 个答案:

没有答案
相关问题