在应用程序处于后台时处理推送通知 (FCM) / 终止 - Android

handle push notification (FCM) while app is in background / kill - Android

我想要什么: 我想从 Firebase 控制台 发送通知。当应用程序处于 background/kill 并且当用户点击 推送通知 时,它可以转到特定的 activity。我们称之为 当前优惠 Activity。当应用程序处于前台时,应该会发生同样的事情。

我的理解: 来自 firebase 文档,当应用程序处于前台时,从 Firebase 控制台发送推送通知 将在 MyFirebaseMessagingService class 的 onMessageReceived 方法中接收。但是当app在background/kill时,MyFirebaseMessagingServiceclass的onMessageReceived方法不会被调用。

我想要什么: 当应用程序处于 background/kill 并且用户点击推送通知时,我想将用户重定向到特定的 activity (当前报价 Activity).

我尝试了 但我没有得到解决方案。

我的代码如下。

MyFirebaseMessagingService.java

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private final String TAG = "mk";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.e(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
    }
}

MyFirebaseInstanceIDService.java

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private final String TAG = "mk";

    @Override
    public void onTokenRefresh() {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Log.e(TAG, "MyFirebaseInstanceIDService Token: " + refreshedToken);
    }
}

ActivityOne.java

    public class ActivityOne extends AppCompatActivity {

    private final String TAG = "mk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Intent intent = getIntent();

        if (intent != null && intent.getExtras() != null) {

            Bundle extras = intent.getExtras();

            for (String key : extras.keySet()) {
                Log.e(TAG, key + " : " + extras.get(key));
            }
        }

        Log.e(TAG, "Token: " + refreshedToken);

        //sendNotificationToPatner();
    }

    private void sendNotificationToPatner() {

        Log.e(TAG, "in sendNotificationToPatner: ");

        SendNotificationModel sendNotificationModel = new SendNotificationModel("check", "i miss you");

        RequestNotificaton requestNotificaton = new RequestNotificaton();
        requestNotificaton.setSendNotificationModel(sendNotificationModel);

        requestNotificaton.setToken("cJ-y3b3mnYk:APA91bEYTp1LtfpMHp-wdvKkq1_dVYF4DXl1SHWIaw0guwPSoTyFtbcJjPPB6GhP0FuRd9ybJ--BM6W3aFB-2-3KpVOBDITX91QV4kH1lAgxsIxHcfaj5FQLOIZ0t_HDC2bd8hqJrH-F4x_wuunJfYzanAagm4xcaA");

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        retrofit2.Call<ResponseBody> responseBodyCall = apiService.sendChatNotification(requestNotificaton);

        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                Log.d(TAG, "in onResponse");
            }

            @Override
            public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
                Log.d(TAG, "in onFailure");
            }
        });
    }
}

提前致谢。

解法:

  1. 如果您从 firebase 控制台发送推送通知并且您的应用程序在后台运行或被终止,那么 MyFirebaseMessagingService 将不会被调用。 (firebase 文档中没有提到)

  2. 如果您从 firebase 控制台发送推送通知并且您的应用程序在前台,那么 MyFirebaseMessagingService 将被调用。

  3. 如果您从服务器发送推送通知(我指的是后端,即网站或某些基于 Web 的仪表板)并且您的应用程序在后台或终止或在前台,那么 MyFirebaseMessagingService 将被调用。

好吧,我来到这里是为了寻找另一个问题的解决方案,但我可以半途而废地回答你的问题。

如果您要发送带有数据负载的通知,那么您可以检查特定附加项的意图。例如,如果在您的控制台 (firebase) 中,您有一个带有某个值 ("This is my message") 的键 ('custom_message'),如下图所示

您可以在您的启动 activity 中收听 Intent 的额外内容,它可以是启动画面或主画面activity。

 Intent intent = getIntent(); 
    if (intent.getStringExtras("custom_message") != null) {
      //go to specific activity when notification is opened
    }
   else{
     //open open default launch activity 
    }

现在,我之前说过我会半回答你的问题。为什么?这是因为我不知道当通知没有数据负载时您将如何实现。希望这对某人有所帮助。

@Maulik Dodia 信息也很有用。赞赏