簡體   English   中英

FCM通知onclick無法打開所需的活動

[英]FCM Notification onclick does not open desired activity

我已經嘗試過這里發布的幾乎所有解決方案以及每個標志的組合,但是它不起作用。

以下是我遇到問題的用例。

1)當我在應用程序中時, FCM通知會打開我想要的活動。 數據傳遞到主要活動中的onNewIntent 當應用程序是前台時,它工作正常。

2)當在后台模式(按下主頁按鈕)上單擊通知后,即使我在清單文件android:launchMode="singleTop"指定為我的主要活動,它也會啟動我的應用程序的新實例,但我不知道發生了什么這里。 我想在這里打開的所需活動是RateEventActivity

這里發生的奇怪的事情是,即使應用程序處於后台模式, NotificationActivityProfileActivity可以正常工作,但是RateEventActivity會產生如上所述的所有問題。 大多數應用程序代碼是由我在RateEevent模塊上工作的其他人編寫的,所以我不知道我在這里缺少什么?

當我在RateEventActivity場景中從后台模式單擊通知時,意圖丟失在某處(在創建時未傳遞給mainActivty )。

我還嘗試將唯一的ID傳遞給我的待定意圖,但沒有運氣。 我花了很多時間和精力來解決這個問題,但每次都失敗

這是我的FCM代碼

  private void createNotification(RemoteMessage remoteMessage) {
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Log.v("Notification Title", remoteMessage.getNotification().getTitle());

    Intent intent = new Intent(this, MainActivity.class);
    //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    if (remoteMessage.getData().containsKey("notificationUser")) {
        intent.putExtra("notificationUser", remoteMessage.getData().get("notificationUser"));
    } else if (remoteMessage.getData().containsKey("notificationId")) {
        intent.putExtra("notificationId", remoteMessage.getData().get("notificationId"));
    } else if (remoteMessage.getData().containsKey("event")) {
        if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Event Feedback")) {


            Log.v("Notification Event", remoteMessage.getData().get("event"));
            intent.putExtra("eventId", remoteMessage.getData().get("event"));
        }


    }


    //PendingIntent.FLAG_UPDATE_CURRENT
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setLargeIcon(largeIcon)
                    .setContentTitle(notification.getTitle())
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(notification.getBody()))
                    .setContentText(notification.getBody())
                    .setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

主要活動代碼onCreate方法

   if (getIntent().hasExtra("notificationId")) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.putExtra("notificationId", getIntent().getStringExtra("notificationId"));
        startActivity(intent);
    } else if (getIntent().hasExtra("user")) {
        Intent intent = new Intent(this, ProfileActivity.class);
        intent.putExtra("userId", getIntent().getStringExtra("user"));
        startActivity(intent);
    } else if (getIntent().hasExtra("eventId")) {
        Intent intent = new Intent(this, RateEventActivity.class);
        intent.putExtra("eventId", getIntent().getStringExtra("eventId"));
        startActivity(intent);
    }

我的清單文件

 <activity
        android:name=".activities.MainActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/HomeTheme" />

<activity
        android:name=".activities.ProfileActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Profile"
        android:screenOrientation="portrait" />

 <activity
        android:name=".activities.NotificationActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Notifications"
        android:screenOrientation="portrait" />

<activity
        android:name=".activities.RateEventActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Rate Users"
        android:screenOrientation="portrait"
       />

如果要打開應用程序並執行特定操作(在后台運行),請在通知有效負載中設置click_action並將其映射到要啟動的活動中的意圖過濾器。 例如,將click_action設置為OPEN_ACTIVITY_1以觸發意圖過濾器,如下所示:

正如react-native-fcm文檔中建議的那樣,請后端以這種形式發送json數據,

 {

    "to":"some_device_token",

    "content_available": true, 
    "notification": {
    "title": "hello",
    "body": "yo",
    "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
    },

"data": {
"extra":"juice"
}
}

並在您的mainfest文件中為您的活動添加意圖過濾器,如下所示

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

當您單擊通知時,它將打開應用程序並直接進入您在click_action中定義的活動,在本例中為“ OPEN_ACTIVTY_1”。 在該活動中,您可以通過以下方式獲取數據:

Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");

請查看以下鏈接以獲取更多幫助:

Firebase FCM通知click_action有效負載

在后台運行應用程序時未調用Firebase onMessageReceived

Firebase控制台:如何為通知指定click_action

我很早以前就收到了這個問題。 讓我檢查我的項目代碼。

啊,在這里。

嘗試設置標志

Intent intent = new Intent(this,    MainActivity.class));       
// set intent so it does not start a new activity
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
         Intent.FLAG_ACTIVITY_SINGLE_TOP);

並如下更改您的PendingIntent

PendingIntent contentIntent = PendingIntent.getActivity(this, ,
                intent, 0);

同時刪除

android:launchMode="singleTop"

來自清單文件中的MainActivity

試試看,讓它起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM