FCM 通知 onclick 未打开所需的活动

use*_*828 2 android android-intent firebase-cloud-messaging

我已经尝试了这里发布的几乎所有解决方案以及每个标志的组合,但它不起作用。

以下是我遇到问题的用例。

1)当我在申请FCM通知打开我想要的活动。数据onNewIntent在主要活动中传递。当应用程序处于前台时它工作正常。

2)当处于后台模式(按下主页按钮)时,单击通知后它会启动我的应用程序的新实例,即使我android:launchMode="singleTop"在清单文件中指定了我的主要活动,但我不知道这里发生了什么。我想在这里打开的所需活动是RateEventActivity

这里发生的奇怪的是,NotificationActivityProfileActivity完美的工作,即使应用程序在后台模式,但RateEventActivity正如我上面描述的是生产的问题。大多数应用程序代码是由我正在处理RateEevent模块的其他人编写的,所以我不知道我在这里遗漏了什么?

mainActivty当我在RateEventActivity场景中从后台模式单击通知时,意图在某处丢失(未传递给创建)。

我也尝试过将唯一 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());
}
Run Code Online (Sandbox Code Playgroud)

主要活动代码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);
    }
Run Code Online (Sandbox Code Playgroud)

我的清单文件

 <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"
       />
Run Code Online (Sandbox Code Playgroud)

Ris*_*esh 5

如果您想打开您的应用并执行特定操作 [在后台运行时],请在通知负载中设置 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"
}
}
Run Code Online (Sandbox Code Playgroud)

并在您的 mainfest 文件中为您的活动添加意图过滤器,如下所示

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

当您单击通知时,它将打开应用程序并直接转到您在 click_action 中定义的活动,在本例中为“OPEN_ACTIVTY_1”。在该活动中,您可以通过以下方式获取数据:

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

查看以下链接以获取更多帮助:

Firebase FCM 通知 click_action 负载

应用程序在后台时未调用 Firebase onMessageReceived

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


归档时间:

查看次数:

5181 次

最近记录:

8 年,9 月 前