猿问

单击 Firebase 通知时打开 MainActivity 以外的其他活动

在我的应用我想打开自定义activity(不MainActivity),并putExtra以该activity点击时火力地堡通知。

我写了下面的代码,但是当点击通知打开MainActivity,但我想打开我的另一个活动(AuctionDetailActivity)。


我的 NotificationManager 类:


public class MyNotificationManager {


    private Context mCtx;

    private Uri soundUri;

    private static MyNotificationManager mInstance;


    public MyNotificationManager(Context context) {

        mCtx = context;

    }


    public static synchronized MyNotificationManager getInstance(Context context) {

        if (mInstance == null) {

            mInstance = new MyNotificationManager(context);

        }

        return mInstance;

    }


    public void displayNotification(String title, String body) {


        soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        Intent intent = new Intent(mCtx, MainActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        intent.putExtra("fcm_notification", "Y");


        PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.NOTIF_CHANNEL_ID)

                .setSmallIcon(R.mipmap.ic_launcher)

                .setContentTitle(title)

                .setSound(soundUri)

                .setAutoCancel(true)

                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})

                .setContentText(body)

                .setContentIntent(pendingIntent);


        NotificationManager mNotifyMgr = (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);


        if (mNotifyMgr != null) {

            mNotifyMgr.notify(1, mBuilder.build());

        }

    }

}


我该如何解决?


杨__羊羊
浏览 179回答 3
3回答

千巷猫影

如果您是从 Firebase 控制台或notification使用 FCM API在现场发送通知,则该应用会以两种方式运行 -如果您的应用程序在前台,onMessageReceived则会调用您的 FCM 服务类的方法。如果您的应用程序在后台运行,则您的 FCM 服务类中不会发生任何事情。相反,通知将由 FCM 库本身在内部处理,并且将显示意图中带有启动器活动的通知。如果您使用 FCM API 发送通知并使用该data字段,则库本身不会执行任何操作,而是调用该方法onMessageReceived,无论您的应用程序是在前台还是后台。因此,为了解决您的问题,您可以使用以下两种解决方案之一:使用 FCM API 发送通知并使用data字段而不是notification字段。查看文档以了解有关 FCM API 的更多信息。在您的启动器(主要)活动中,检查内部的意图onCreate,如果它来自通知,请阅读附加内容,完成主要活动并打开所需的活动。第二种情况的示例:@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    if (checkIntent()) return;    // other code.}@Overrideprotected void onNewIntent(Intent intent) {    super.onNewIntent(intent);    checkIntent();}private boolean checkIntent() {    // to receive the value, send the value as custom data from Firebase console.    String value = getIntent().getStringExtra("your_key");    if (value == null) return false;    if (value.equals("something")) {        // open one activity.    } else if (value.equals("another_thing")) {        // open another activity.    }    finish();    return true;}

侃侃无极

更改以下行Intent intent = new Intent(click_action);对此Intent intent = new Intent(getActivity(), YourClass.class);
随时随地看视频慕课网APP

相关分类

Java
我要回答