0

我知道这是问过一百万次之前,我已经尝试了所有我看到的,但我不能让它的工作。我有数据有效负载通知。我在onMessageReceived(),但当我点击通知托盘中的通知它不会重定向到启动程序。FCM数据有效载荷通知不会打开提到的活动

if (remoteMessage.getData().size() > 0) { 
     Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 
     JSONObject data = json.getJSONObject("data"); 
     Intent resultIntent = new Intent(getApplicationContext(), LandingActivity.class); 

     Bundle bundle = new Bundle(); 
     bundle.putString("data", data.toString()); 
     resultIntent.putExtras(bundle); 


      // image is present, show notification with image 
      //showNotificationMessageWithBigImage(getApplicationContext(), title, message, "13", resultIntent, profilePicThumb); 
      Intent intent = new Intent(this, LandingActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
        PendingIntent.FLAG_ONE_SHOT); 

      Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setContentTitle("FCM Message") 
        .setContentText("my message") 
        .setAutoCancel(true) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

      NotificationManager notificationManager = 
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 

LandingActivity是我的应用程序的发射活动。

我的manifest.xml

<activity 
     android:name=".landing.LandingActivity_" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.FullScreen"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

当给出可选data_payload notification_payload我LandingActivity了数据,但是当我得到的只有data_payload点击redriection犯规发生

数据有效载荷的结构是这样的

"data" : 
{ 
"notification_type" : "QUIZ_WINNER", 
"notification_message": "You hav won the quiz ", 
"notification_data" : [ 
     "user_id"   : id, 
     "full_name"  : full_name, 
     "profile_pic_thumb": user_profile_picthumb, 
     'quiz_id'   : quiz_id, 
     'question_id'  : question_id, 
     'quiz_title'  : quiz_title, 
    ], 
"user_id"    : from_id, 
'notification_to'  : to_id, 
'created_at'   : date, 
'updated_at'   : date, 

}

我能够收到通知,但无法打开LandingActivity

请帮忙。 在此先感谢。

最后的答案

最后我得到了它working..I使用Android的注解throught我的项目。我需要打开的活动也被注释了。因此,而不是这行代码

Intent intent = new Intent(this, LandingActivity.class); 

的我加入这个

Intent intent = new Intent(this, LandingActivity_.class);

希望这将是有用的人在某个时候。

+0

您可以向我们展示函数showNotificationMessage的内容以及有效负载的样本,无论是否带有可选数据。 –

+0

@JudeFernandes请看我的帖子。我已经添加了有效载荷。 showNotificationMessage()不再使用 – suja

+0

Hi @suja请添加答案。在堆栈溢出中,高度鼓励自我回答。我看到您之前添加了一个,您可以继续并取消删除,然后在几个小时后再接受它。 :) –

回答

0

在您正在设置'待处理意向'的行中,您应该使用非零的请求代码。它解决了我的问题。我希望它能解决你的问题。

int requestCode = (int) System.currentTimeMillis(); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 
+0

我会试一试并更新你 – suja

+0

不行不行。无论如何感谢您的帮助。 – suja