确定是否从FCM通知点击中打开了活动 [英] Identify if an activity was opened from FCM notification click

查看:110
本文介绍了确定是否从FCM通知点击中打开了活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the fcm console to send messages to all of devices that have my app installed, the notifications don't have any extra payload only the notification message.

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

I want to know if there is a simple way to know if an Activity was opened from an FCM notification click.

有一个解决方案,可以扩展FirebaseMessagingService服务并自己创建通知.

There is a solution for that by extending the FirebaseMessagingService service and creating a notification by yourself.

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

I want to know if there is another solution without extending the service or passing extras to the notification.

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

Is there any extra that is being passed with the Intent that is used for opening the activity from the notification click?

推荐答案

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

I hope you know that FCM based messaging created two types of notification

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

Firstly, the notification that we create from on onMessageReceived() method, the point to note here is that onMessageReceived() will be triggered if the app is in foreground.

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

Secondly, FCM creates its own default notification when app is in background, in this case onMessageReceived() is not intercepted and hence we cannot set a pending intent on our own.

注意:当我向应用程序发送通知"推送消息时,如果出现数据"消息onMessageReceived(),则无论上述应用程序处于前台状态,上述类型都会起作用或背景(从FCM控制台发送的通知是通知"类型的推送消息)

Note : the above mentioned types comes to play when you are sending a "notification" push message to your app in the case of a me "data" message onMessageReceived() is triggered irrespective of the app being in foreground or background (the notifications sent from FCM console are "notifications" type push messages)

回到您的问题,您是否要从FCM控制台发送推送消息或发出FCM服务器请求还不清楚,所以让我们在这里举例说明.

Coming back to your question, it is not clear if you're sending a push message from FCM console or making a FCM server request, so let's make cases here.

  1. FCM控制台正在发送消息:
    从FCM的通知面板中的高级"部分发送数据有效载荷
  1. Message being sent by FCM Console :
    send data payload from the advance section in notification panel on FCM which looks like this

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

when app is in foreground onMessageReceived() will be intercepted use the code below to receive the data payload

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;
}
}

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

and if app is in background, then on notification click app will be opened on a "default" activity, you can set any activity as a default one by adding the following lines in the app manifest in the activity's intent filter:

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

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

inside this activity you can call to get intent extras and then get the data payload to decide on which activity you need to land on. The code is below

    .
    .
    .

        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服务器相同:

  1. Message sent from FCM Server: the message sent from FCM consolse above is same as sending the below json body as post request to FCM server:

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

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

the process of intercepting the notifications will be same for this case.

这篇关于确定是否从FCM通知点击中打开了活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆