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