0

這是我的代碼,當我的應用程序在前臺運行時,它會打開有針對性的活動,但是當應用程序關閉或在後臺時,它不會打開目標活動,請幫助我解決這個問題點擊FCM通知打開目前關閉的活動沒有針對性的活動

我希望通過點擊通知將打開目標活動甚至應用程序正在運行/關閉。

public void onMessageReceived(RemoteMessage remoteMessage) { 

    session = new Session(this); 
    // if (session.getBscloggedin() || session.getAdploggedin()) { 
     // Log.d(TAG, "FROM:" + remoteMessage.getFrom()); 

     //Check if the message contains data 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data: " + remoteMessage.getData()); 
     } 

     //Check if the message contains notification 

     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody()); 
      sendNotification(remoteMessage.getNotification().getBody()); 
     } 
    } 


/** 
* Dispay the notification 
* @param body 
*/ 


private void sendNotification(String body) { 

    //for CS101 announcement notification 
    if (body.contains("CS101")) { 
     Intent intent = new Intent(this, CS101noti.class); 
     intent.putExtra("body", body); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, 0); 
     //Set sound of notifica tion 
     Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("mine") 
       .setContentText(body) 
       .setAutoCancel(true) 
       .setSound(notificationSound) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build()); 
    } 
+0

https://stackoverflow.com/questions/13716723/open-application-after-clicking-on-notification檢查此鏈接 –

回答

0

onMessageReceived回調稱爲傳遞活動的完全合格的類路徑,應打開通知消息在後臺。如果您在後臺收到此類消息,則只能在啓動器活動中處理該消息。 enter image description here

解決方法是更改​​後端,以便它僅發送數據消息,並在內部處理它們,或者在啓動器活動中寫入一些路由代碼。

0

使用「click_action」從你的後端從您發送推送通知的屬性。在屬性值,你當你點擊那個notification.for更多的參考是指當你收到handling firebase push

0
Intent intent = new Intent(this, CS101noti.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
.... 
0

請求代碼應該是唯一的。

一般人們與我們分享例如給予請求代碼0(零)。我有時候是複製/粘貼開發者之一。但最近我遇到了使用這條線的錯誤。

PendingIntent.getActivity(myContext, 0, myIntent, 0); 

每當應用程序收到通知時,intent活動都會因requestCode接收到舊數據。這裏是關於該方法的文檔。

返回與給定的 參數匹配的現有或新的PendingIntent。

所以第二個參數(requestCode)應該是唯一的intent。如果您爲每個通知使用相同的請求代碼,則活動將接收舊數據。所以你的requestCode應該是唯一的。

還有另一種解決方案。使用PendingIntent.FLAG_UPDATE_CURRENT作爲標誌。該文件說,

如果所描述的PendingIntent已經存在,則保留它,但 什麼是在這個新的意圖替換其額外數據。

+0

你能解釋一下第二個參數(requestCode)應該是唯一的intent嗎? –

+0

請求代碼在您的意圖中應該是唯一的。否則它會創建意圖相同的請求代碼,這意味着打開相同的活動 – mertsimsek

+0

我試過這個,但沒有工作PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT) –

相關問題