2017-05-29 68 views
0

我想通過FCM控制檯生成通知,我收到它們,但我無法覆蓋FirebaseMessagingService的onMessageReceived。不知道我做錯了什麼。FCM通知沒有打開預期的活動Android

MyFirebaseMessagingService類負責處理通知:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     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(),remoteMessage.getData()); 
     } 
    } 

    /** 
    * Dispay the notification 
    * @param body 
    */ 
    private void sendNotification(String body , Map<String,String> data) { 

//  int finalSecId = Integer.parseInt((String) data.get("sec_id")); 
//  int sec = Integer.parseInt((String) data.get("sec")); 
     Intent intent = new Intent(this, InsuranceActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT); 
     //Set sound of notification 
     Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.login_meter) 
       .setContentTitle(getString(R.string.app_name)) 
       .setContentText((String) data.get("sec_id")+ " "+(String) data.get("sec")) 
       .setAutoCancel(true) 
       .setSound(notificationSound) 
       .setContentIntent(pendingIntent); 

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

而且內部應用標籤

<service android:name=".Fcm.MyFirebaseMessagingService" 
      android:enabled="true" 
      android:exported="true" 
      > 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 
     <service android:name=".Fcm.MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service> 
+0

您需要從服務器和信息數據發送數據,而不是通知你要顯示通知 –

+0

實際上,數據從FCM控制檯發送,但我無法處理它在服務onMessageReceived不會被觸發,我無法看到具體標籤的結果日誌以及 –

+0

然後在fcm控制檯,使用ADVANCED選項並通過鍵值對發送數據。之後,你可以在OnMessageReceived() –

回答

1

擴展#Sudip吊艙器的意見和#Ratilal喬普達回答 遵循以下步驟:

第一步:

<activity android:name=".SplashActivity"> 
     <intent-filter> 
      <action android:name=".SplashActivity" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

第二步:

我使用PHP的服務器,所以你需要按照自己喜歡的方式進行調整,但在通知有效負載中添加"click_action" : ".SplashActivity"

$fields = array(
      'to' => $token, 
      'notification' => array(
       'title' => 'Motors City', 
       'body' => $message, 
       "click_action" => ".AppSplash", 

      ), 
      'data' => array(
       'sec_id' => $secID, 
       'sec' => $sec, 
       'extra1'=>$extra1, 
       'extra2'=>$extra2 
      ) 
     ); 

     $headers = array(
      'Authorization:key=' . $server_key, 
      'Content-Type:application/json' 
     ); 

第三步: 在OnCreate中的你SplashActivity

Bundle bundle = getIntent().getExtras(); 
if (bundle != null) { 
    Log.d(TAG,bundle.toString); 
}} 

和你做

+1

寫得很好(Y) –

1

有兩種類型的FCM的

通知消息:發送有效載荷與此消息類型觸發onMessageReceived()僅當您的應用程序處於前景時。

data消息:無論您的應用程序位於前臺/後臺,只發送此特定消息類型的有效內容都會觸發onMessageReceived()。

參考:here

-1

現在您有在通知類型,這將觸發通知默認,只要打開該通知的點擊應用程序通知試試這個

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); 
+0

我想你已經讀過代碼中已經提到的代碼 –

+0

in your code intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); –

+0

而不是setFlags使用addFlags –

0

因此,您需要將服務器端代碼從通知類型更改爲數據類型。 並嘗試從

`remoteMessage.getData()` not from `remoteMessage.getNotification()` 

得到消息,如果你要管理的通知使用的數據類型的點擊notification.to瞭解更多關於此類型通過這個鏈接 https://firebase.google.com/docs/cloud-messaging/concept-options

0

我不得不處理這個問題來自FCM的通知。

首先,我們必須瞭解有兩種類型的通知。

  1. 通知 - 當您的應用不在前臺併產生通知時,它會觸發。如果你點擊它,它會打開啓動器的活動。

  2. 數據通知 - 這是用來解析數據,它是在後臺和前臺接收的。因此,您可以基於FCM Push中數據對象中提供的數據構建自定義通知。

    Map<String ,String> dataMap = remoteMessage.getData(); 
    

    在這裏我創建了一個簡單的鍵值對映射。現在,我可以在數據對象中收到通知的標題,並使用自定義意圖進行簡單通知。

我個人使用上下文對象來確定應用程序是在前臺還是後臺。基於此,我決定是否必須顯示通知或僅更新數據。

希望這會有所幫助。