0

我知道有這樣的多個帖子,但我已經嘗試了很多解決方案,沒有爲我工作。在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) } 
+0

嘗試調用外sendNotification的()方法,如果條件因爲的GetNotification()可能返回null如果應用程序是在後臺。 –

+0

請參閱:http://stackoverflow.com/questions/37565599/firebase-cloud-messaging-fcm-launch-activity-when-user-clicks-the-notificati – rafsanahmad007

+0

該代碼看起來對我來說沒問題。你可以發佈你的服務器端代碼或樣本有效載荷? –

回答

3

如果應用程序在後臺,通知引導到系統托盤中。

點擊通知後,它將重定向到清單文件中給出的啓動器活動。

從發射器活動中,我們可以調用任何活動,但首先它會啓動發射器活動。

在啓動器活動中,我們可以獲取通知消息內容。

AndroidManifest.xml中

<activity 

      android:name=".view.SplashActivity" 

      android:screenOrientation="portrait"> 

      <intent-filter> 

       <action android:name="android.intent.action.MAIN" /> 



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

      </intent-filter> 

     </activity> 

SplashActivity.Java

public class SplashActivity extends AppCompatActivity { 



    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash); 
     Bundle bundle = getIntent().getExtras(); 

     if (bundle != null && bundle.get("data")!=null) { 


    //here can get notification message 
      String datas = bundle.get("data").toString(); 

    } 
} 
} 
+0

這裏沒有使用Splash Activity:/ –

相關問題