我知道有這樣的多個帖子,但我已經嘗試了很多解決方案,沒有爲我工作。在FCM通知點擊Android後打開活動
我正在使用Firebase雲消息傳遞發送通知。通知到達,但它打開主要活動,而不是我想要的。
我嘗試着嘗試,但是在開始適當的活動時仍然失敗。試過不同的意圖或PendingIntent設置,嘗試click_action,沒有爲我工作。
我認爲它甚至沒有進入我的FirebaseMessagingService,但我完成了Firebase指令中的所有操作。
清單:
<service
android:name="com.packagename.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name="com.packagename.NotificationActivity">
<action android:name="OPEN_ACTIVITY_1" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
FirebaseMessagingService:
點擊後public class FirebaseMessagingService extends FirebaseMessagingService {
private static String TAG = "Firebase";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if(remoteMessage.getNotification()!=null){
Log.d(TAG,"Message body : "+remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}
}
private void sendNotification(String body, String title) {
Intent notificationIntent = new Intent(this, NotificationActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("notification", body);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body)
.setBigContentTitle(title))
.setContentText(body)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_SOUND);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notification.build());
}
}
日誌:
D/StatusBar: Clicked on content of 0|com.mypackagename|0|GCM-Notification:89893881|10177 , PendingIntent{cc15013: [email protected]} , Intent { act=com.google.firebase.MESSAGING_EVENT cmp=com.mypackagename/com.google.firebase.iid.FirebaseInstanceIdInternalReceiver (has extras) }
嘗試調用外sendNotification的()方法,如果條件因爲的GetNotification()可能返回null如果應用程序是在後臺。 –
請參閱:http://stackoverflow.com/questions/37565599/firebase-cloud-messaging-fcm-launch-activity-when-user-clicks-the-notificati – rafsanahmad007
該代碼看起來對我來說沒問題。你可以發佈你的服務器端代碼或樣本有效載荷? –