确定是否通过FCM通知单击打开了活动

问题描述 投票:7回答:3

我正在使用fcm控制台将消息发送到所有安装了我的应用程序的设备,通知仅通知消息就没有任何额外的负载。

我想知道是否有一种简单的方法可以知道是否通过FCM通知单击打开了活动。

[通过扩展FirebaseMessagingService服务并自己创建通知来解决此问题。

我想知道是否存在另一种解决方案,而无需扩展服务或将其他功能传递给通知。

是否有与Intent一起传递的用于从通知点击中打开活动的任何额外信息?

android firebase-cloud-messaging
3个回答
10
投票

我希望您知道基于FCM的消息传递创建了两种类型的通知

首先,我们从onMessageReceived()方法创建的通知,这里要注意的是,如果应用程序处于前台,则将触发onMessageReceived()

其次,当应用程序在后台时,FCM将创建其自己的默认通知,在这种情况下,onMessageReceived()不会被拦截,因此我们无法自行设置待处理的意图。

注意:当我向应用程序发送“通知”推送消息到我时,上面提到的类型开始播放,无论是否处于前台或后台,都会触发“数据”消息onMessageReceived() (从FCM控制台发送的通知是“通知”类型的推送消息)

回到您的问题,尚不清楚您是从FCM控制台发送推送消息还是向FCM服务器发出请求,因此让我们在这里举例说明。

  1. FCM控制台发送的消息:从FCM的通知面板中的“高级”部分发送数据有效载荷,如下所示:>
  2. enter image description here

当应用程序处于前台onMessageReceived()时将被拦截使用下面的代码来接收数据有效载荷

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //Displaying data in log
    //It is optional


    //Calling method to generate notification
    sendNotification(remoteMessage.getData());
}

//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(Map<String, String> messageBody) {

    Intent intent = new Intent(this, SplashScreen.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this,
            0,
            setNotificationRoute(messageBody),
            PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.sym_def_app_icon)
            .setContentTitle(messageBody.get("title"))
            .setContentText(messageBody.get("text"))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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


}

private Intent setNotificationRoute(Map<String, String> messageBody) {
    Intent intent = null;
    String type = messageBody.get("type");
    if (type != null) {
        switch (type) {
            case "android":    //intercept your payload here to create swit 
                intent = new Intent(this, MainActivity.class);
                break;
            default:
                break;

        }

    }
    return intent;
}
}

如果应用程序处于后台,则在通知单击时,将在“默认”活动上打开应用程序,您可以通过在活动清单的意图过滤器中的应用程序清单中添加以下几行来将任何活动设置为默认活动:

<category android:name="android.intent.category.DEFAULT" />

在此活动中,您可以调用以获取额外的意图,然后获取数据有效负载来确定您需要进行的活动。代码如下

    .
    .
    .

        Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                setNotificationRoute(getIntent().getExtras());
    .
    .
}
 private void setNotificationRoute(Bundle extras) {
    String type = extras.getString("type");
    Intent intent = null;
    if (type != null) {
        switch (type) {
            case "message":
                String room = extras.getString("room");
                intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
      }
    }
  1. 从FCM服务器发送的消息:上面从FCM consolse发送的消息与将以下json正文作为后请求发送到FCM服务器相同:

{ 
    "notification": {
    "title": "Hi Tester",
    "text": "News for testing",
    "sound": "default",
    "badge": 0
  },
  "data":{
    "type": "credits"
  },
  "priority": "high",
  "to": "{{device_token}}"
}

在这种情况下,拦截通知的过程将是相同的。


0
投票

如果您想在不扩展FireBaseMessaging类的情况下启动任何活动,则可以使用FCM数据有效负载设置此标志。


-3
投票

无需通过任何额外的意图。只需设置标志,如

© www.soinside.com 2019 - 2024. All rights reserved.