在应用程序处于后台/终止状态时处理推送通知(FCM)-Android [英] handle push notification (FCM) while app is in background / kill - Android

查看:682
本文介绍了在应用程序处于后台/终止状态时处理推送通知(FCM)-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的内容:我想从 Firebase控制台发送通知.当应用程序处于后台/终止状态并且用户点击推送通知时,它可以转到特定活动.我们称之为当前优惠活动.当应用程序位于前台时,应该发生同样的事情.

What I want: I want to send notification from Firebase Console. When app is in background/kill and when user taps on Push Notification it can go to particular activity. Let's call it Current Offers Activity. Same thing should happen while app will be in foreground.

我的理解:从 firebase文档中,当应用程序处于前台状态时, Firebase控制台发送的推送通知将在 MyFirebaseMessagingService类的方法.但是,当应用程序处于后台/kill状态时,MyFirebaseMessagingService类的onMessageReceived方法将不会被调用.

What I understood: From firebase docs, when app is in foreground, push notifications sent from Firebase Console will be received in onMessageReceived method of MyFirebaseMessagingService class. But when app is in background/kill, the onMessageReceived method of MyFirebaseMessagingService class won't get called.

我想要的东西:当应用程序处于后台/状态时,&用户点击推送通知,我想将用户重定向到特定活动(当前优惠活动).

What I want: When app is in background/kill & user taps on push notification, I want to redirect user to particular activity (Current Offers Activity).

我尝试了此答案但我没有解决方案.

I tried this answer but I did not get the solution.

我的代码如下.

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文档中未提及)

如果您是从Firebase控制台发送推送通知&您的应用程序处于前台,则 MyFirebaseMessagingService 将被调用.

If you are sending push notification from firebase console & your app is in foreground then MyFirebaseMessagingService will get called.

如果您要从服务器发送推送通知(我的意思是后端,即网站或某些基于Web的仪表板),您的应用处于后台,处于死机状态或处于前台,则 MyFirebaseMessagingService 将被调用.

If you are sending push notification from your server(I mean back-end i.e a website or some web based dashboard) & your app is in background or kill or in foreground then MyFirebaseMessagingService will get called.

这篇关于在应用程序处于后台/终止状态时处理推送通知(FCM)-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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