亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊 Firebase 通知時打開 MainActivity 以外的其他活動

單擊 Firebase 通知時打開 MainActivity 以外的其他活動

楊__羊羊 2021-10-20 11:25:59
在我的應用我想打開自定義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());        }    }}我該如何解決?
查看完整描述

3 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

如果您是從 Firebase 控制臺或notification使用 FCM API在現場發送通知,則該應用會以兩種方式運行 -

  • 如果您的應用程序在前臺,onMessageReceived則會調用您的 FCM 服務類的方法。

  • 如果您的應用程序在后臺運行,則您的 FCM 服務類中不會發生任何事情。相反,通知將由 FCM 庫本身在內部處理,并且將顯示意圖中帶有啟動器活動的通知。

如果您使用 FCM API 發送通知并使用該data字段,則庫本身不會執行任何操作,而是調用該方法onMessageReceived,無論您的應用程序是在前臺還是后臺。

因此,為了解決您的問題,您可以使用以下兩種解決方案之一:

  • 使用 FCM API 發送通知并使用data字段而不是notification字段。查看文檔以了解有關 FCM API 的更多信息。

  • 在您的啟動器(主要)活動中,檢查內部的意圖onCreate,如果它來自通知,請閱讀附加內容,完成主要活動并打開所需的活動。

第二種情況的示例:

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    if (checkIntent()) return;


    // other code.

}


@Override

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

}


查看完整回答
反對 回復 2021-10-20
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

更改以下行

Intent intent = new Intent(click_action);

對此

Intent intent = new Intent(getActivity(), YourClass.class);


查看完整回答
反對 回復 2021-10-20
  • 3 回答
  • 0 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號