0

我在我的android应用程序中使用FCM通知。当应用程序在后台时,通知单击导航到MainActivity。只有当只有一个通知时才会发生这种情况。当有多个通知时,只有最后一个通知才能启动应用程序。 一旦应用程序进入前台,通知点击不会发生。如果我关闭应用程序并单击通知栏中的下一个通知,则应用程序会再次打开。处理多个推送通知在Android中点击

我认为通知有效负载中的通知操作存在问题。

<application 
    android:name=".MyApplication" 
    android:allowBackup="false" 
    android:icon="@mipmap/ic_launcher" 
    android:launchMode="singleTop" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    tools:replace="android:allowBackup"> 

    <!-- tools:replace="android:theme" --> 
    <!-- Launch Flow --> 
    <activity 
     android:name=".ui.activity.MainActivity" 
     android:screenOrientation="portrait" 
     android:theme="@style/SplashTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

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


    <!-- [START firebase_service] --> 
    <service android:name=".service.MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_service] --> 


    <!-- [START firebase_iid_service] --> 
    <service android:name=".service.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_iid_service] --> 


</application> 

这是我的通知json。

{ 

"notification":{ 
    "title":"Jogn Doe", 
    "body":"Jogn commented on your post", 
    "sound":"mySound", 
    "tag":"88211407420864", 
    "click_action":"myAction" 
}, 
"data":{ 
    "notificationCount":2, 
    "creatorName":"John Doe", 
    "creatorImage":"https://image.com/sample.png", 
    "notificationId":"88335540289536", 
    "universityId":"8821098664448" 
}, 
    "to":"fYu_ybp3Wu8:APA91bHxOact-9gcqf7rAqNSPSkvU7N5h4t4m0XgCAHBdu" 
} 

我这样做是onCreate方法..

Intent intent = new Intent(LauncherActivity.this, PostFeedActivity.class); 
    if (getIntent().getAction().equalsIgnoreCase("myAction")) { 
     Bundle extras = getIntent().getExtras(); 
     String universityId = extras.getString(getString(R.string.notification_university)); 

     String notificationId = extras.getString(getString(R.string.notification_id)); 
     String notificationCreatorName = extras.getString(getString(R.string.notification_creator_name)); 
     String notificationCreatorImage = extras.getString(getString(R.string.notification_creator_image)); 
     int notificationCount = extras.getInt(getString(R.string.notification_count), 0); 
     if (!TextUtils.isEmpty(notificationId)) { 
      intent.setAction(getString(R.string.action_notification_from_push)); 

      intent.putExtra(getString(R.string.notification_count), 
        notificationCount); 
      intent.putExtra(getString(R.string.notification_id), 
        notificationId); 
      intent.putExtra(getString(R.string.notification_creator_name), 
        notificationCreatorName); 
      intent.putExtra(getString(R.string.notification_creator_image), 
        notificationCreatorImage); 

      //fetch notification data by calling API and start activity 
      requestNotification(notificationId, intent); 
      // 

     } else { 
      startActivity(intent); 
      finish(); 
     } 

回答

0

如果所有通知有它们由Android分组的相同tag,然后你只需要提供的最新通知。 我无法从您的代码中看到,您的tag是否是唯一值(因此您可以在设备的通知栏中并排显示它们)。

只要你有一个notification对象并不仅data,机器人需要通知的部分控制,是指:

  • 如果您的应用程序不运行或在后台,通知显示通知栏
  • 如果您的应用程序是在前台运行,没有通知所示,相反,回调onMessageReceived回调直接调用您的FirebaseMessagingService(这是也是调用的回调,如果你收到来自FCM纯数据报文)

也看到这里FCM notification merge

希望这有助于 欢呼

+0

当应用程序是在前台,通知是否正常工作。但是当我的应用程序在后台运行时出现我的问题。 **标签**是独一无二的。 –

+0

你可以在myAction中显示代码吗?你如何处理点击? – Grisgram

+0

我加我的问题:) –