2017-05-02 32 views
1

當應用處於前臺時,無法接收推送通知。在後臺收到推送通知,但收到推送時無法獲取數據。它是由FCM無法從onMessageReceived方法中使用fcm通知中獲取數據

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     Log.e("inside","inside"); 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     // Check if message contains a data payload. 
     JSONObject object=new JSONObject(remoteMessage.getData()); 
     Log.e("jsonobject",object.toString()); 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
      sendNotification(remoteMessage.getData().toString()); 

//   if (/* Check if data needs to be processed by long running job */ true) { 
//    // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher. 
//    scheduleJob(); 
//   } else { 
//    // Handle message within 10 seconds 
//    handleNow(); 
// 
//   } 

     } 

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

     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
    } 
    // [END receive_message] 

    /** 
    * Schedule a job using FirebaseJobDispatcher. 
    */ 
    private void scheduleJob() { 
     // [START dispatch_job] 
     FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this)); 
     Job myJob = dispatcher.newJobBuilder() 
       .setService(MyJobService.class) 
       .setTag("my-job-tag") 
       .build(); 
     dispatcher.schedule(myJob); 
     // [END dispatch_job] 
    } 

    /** 
    * Handle time allotted to BroadcastReceivers. 
    */ 
    private void handleNow() { 
     Log.d(TAG, "Short lived task is done."); 
    } 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     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.ic_launcher) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

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

自動生成的,我也得到這個

W/ContextImpl:forgetServiceDispatcher失敗: java.lang.IllegalArgumentException異常:未註冊服務: com.google.android。 [email protected]

java.lang.IllegalArgumentException: Service not registered: [email protected] 
                      at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1105) 
                      at android.app.ContextImpl.unbindService(ContextImpl.java:1872) 
                      at android.content.ContextWrapper.unbindService(ContextWrapper.java:562) 
                      at com.google.android.gms.common.stats.zzb.zza(Unknown Source) 
                      at com.google.android.gms.measurement.internal.zzad.disconnect(Unknown Source) 
                      at com.google.android.gms.measurement.internal.zzad.zzabl(Unknown Source) 
                      at com.google.android.gms.measurement.internal.zzad.zzb(Unknown Source) 
                      at com.google.android.gms.measurement.internal.zzad$1.run(Unknown Source) 
                      at com.google.android.gms.measurement.internal.zzf$1.run(Unknown Source) 
                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                      at com.google.android.gms.measurement.internal.zzw$zzd.run(Unknown Source) 
+0

這是什麼'Log.e( 「的JSONObject」 ,object.toString());'...顯示? – rafsanahmad007

+0

我正在嘗試打印從remoteMessage獲取的有效內容 –

回答

0

請我如果您使用其他Google服務,則版本相同。看文件的build.gradle(模塊:APP)

compile 'com.google.android.gms:play-services-location:10.2.4' 
apply plugin: 'com.google.gms.google-services' 
compile 'com.google.firebase:firebase-messaging:10.2.4' 

檢查清單文件是類似這樣的:

 ...<activity android:name=".actMyLastActivityFromManifest"></activity> 

    <service 
     android:name=".MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".MyFirebaseInstanceIdService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service> 
</application>