从Firebase推送通知时打开特定活动 [英] open specific activity when e push notifications from firebase

查看:62
本文介绍了从Firebase推送通知时打开特定活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,并试图接收推送通知.我的问题是,点击保留的通知后,打开主要活动,我需要从firebase收到推送通知时直接打开特定活动.这是我的代码

I have android application and tried to receive a push notifications. My problem when click the reserved notification open the main activity, I need when received push notifications from firebase open a specific activity directly. This is my code

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody());
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e("Mo", "Exception: " + e.getMessage());
        }
        Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
        startActivity(resultIntent);
    }

    private void handleDataMessage(JSONObject json) {

        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();

            Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
            startActivity(resultIntent);
        } else {
            Intent resultIntent = new Intent(getApplicationContext(), Notification_Page.class);
            startActivity(resultIntent);
        }
    }

推荐答案

您可以在通知中设置待处理的意图.因此,首先创建一个待处理的意图:

You can set a pending intent in to your notification. So first create a pending intent:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), 0);

然后将其添加到您的 NotificationBuilder 中,您可以在其中定义通知的标题,描述等,并使用以下参数:

Then add to your NotificationBuilder, where you define the title, description, etc.. of the notification, the following parameter:

builder.setContentIntent(contentIntent)

更新:

您的代码中已经有您的 NotificationCompatBuilder .

You have your NotificationCompatBuilder already in your code.

替换此:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody());

与此:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Destination.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.app_logo_a)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getNotification().getBody())
                .setContentIntent(contentIntent);

这篇关于从Firebase推送通知时打开特定活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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