2017-06-01 134 views
1

有在FCM(火力地堡雲消息)兩種類型的消息:打開特定活動時通知在後臺點擊

顯示信息:這些消息只觸發onMessageReceived()回調時,你的應用程序是在前臺

數據消息: 論文的消息觸發,即使你的應用程序是在前臺/後臺onMessageReceived()回調/時在前臺接到通知遇難

我能夠打開通知按特定的活動,但在通知收到背景我可以T處理通知按(它會自動啓動的主要活動)

我做了一些搜索,發現:

我添加了這些意向過濾器,以我的通知活動:

<activity android:name="com.mahdi.tiger.alahedclubnewtesting.activity.News_description" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="com.mahdi.tiger.alahedclubnewtesting.activity.News_description"></action> 
       <category android:name="android.intent.category.DEFAULT"></category> 
      </intent-filter> 
     </activity> 

但它不將不起作用..

我還發現:

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. 

那我該怎麼辦?我希望在後臺收到通知來處理點擊並打開我的具體活動。

這是我使用的代碼:

public class myFirebaseMessagingService extends FirebaseMessagingService { 

     private static final String TAG = "FirebaseMessageService"; 
     Bitmap bitmap; 

     /** 
     * Called when message is received. 
     * 
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
     */ 
     @Override 
     public void onMessageReceived(RemoteMessage remoteMessage) { 
      // There are two types of messages data messages and notification messages. Data messages are handled 
      // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
      // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
      // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
      // When the user taps on the notification they are returned to the app. Messages containing both notification 
      // and data payloads are treated as notification messages. The Firebase console always sends notification 
      // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options 
      // 
      Log.d(TAG, "From: " + remoteMessage.getFrom()); 

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

      // Check if message contains a notification payload. 
      if (remoteMessage.getNotification() != null) { 
       Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 

       String title=remoteMessage.getNotification().getTitle(); 
       String body=remoteMessage.getNotification().getBody(); 
       String action=remoteMessage.getNotification().getClickAction(); 



       sendNotification2(title,body,action); 
      } 

      //The message which i send will have keys named [message, image, AnotherActivity] and corresponding values. 
      //You can change as per the requirement. 
      String text = remoteMessage.getData().get("title"); 
      //message will contain the Push Message 
      String message = remoteMessage.getData().get("message"); 
      //imageUri will contain URL of the image to be displayed with Notification 
      //If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened. 
      //If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity will be opened. 
      String TrueOrFlase = remoteMessage.getData().get("AnotherActivity"); 

      //To get a Bitmap image from the URL received 
      sendNotification(message, text,TrueOrFlase); 

     } 


     /** 
     * Create and show a simple notification containing the received FCM message. 
     */ 

     private void sendNotification(String messageBody,String text, String TrueOrFalse) { 
      Intent intent = new Intent(this, Slider_description.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.putExtra("AnotherActivity", TrueOrFalse); 
      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.ahed_me) 
        .setContentTitle(text) 
        .setAutoCancel(true) 
        .setContentText(messageBody) 
        .setSound(defaultSoundUri) 
        .setContentIntent(pendingIntent); 

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

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

注:我使用火力地堡控制檯發送通知。

非常感謝。

+0

您嘗試啓動的活動的名稱是什麼? –

+0

News_description activity –

回答

1

經過一些測試,我發現這一點。

https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java

爲你的有趣的部分。

有兩種類型的消息數據消息和通知 消息。數據消息在onMessageReceived中處理,無論是 該應用程序在前臺還是後臺。數據消息是傳統上與GCM一起使用的 類型。當應用處於前臺時,通知消息僅在onMessageReceived中收到 。當 應用程序在後臺時,自動生成的通知顯示爲 。當用戶點擊通知時,他們將返回到 該應用程序。包含通知和數據有效載荷的消息是 作爲通知消息處理。 Firebase控制檯始終會發送 通知消息。

如果你想在你需要發送數據報文通知完全控制。根據我的理解,您無法使用Firebase控制檯執行此操作。

+0

不,不要緊,因爲我最近更改了Slider_description到News_description –

+0

@MahdiHraybi你試過用數據信息嗎? –

+0

我已經找到了解決方案謝謝@Kevin,但現在我面臨的另一個問題是,當發送另一個通知時,它將替換第一個 –

相關問題