2017-05-02 1 views
1

Die Push-Benachrichtigung kann nicht empfangen werden, wenn sich die Anwendung im Vordergrund befindet. Außerdem wird die Push-Benachrichtigung im Hintergrund empfangen, kann aber die Daten nicht abrufen, wenn Push empfangen wird. Es wird automatisch generiert durch FCMFehler beim Abrufen von Daten aus der Benachrichtigung mit fcm in der Methode 'onMessageReceived'

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()); 
    } 
} 

ich auch immer bin dieses

W/ContextImpl: forgetServiceDispatcher fehlgeschlagen für: java.lang.IllegalArgumentException: Dienst nicht registriert: 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

was ist das 'Log.e ("JSONObject" , object.toString()); '... anzeigen? – rafsanahmad007

+0

Ich versuche, die Payload von RemoteMessage zu drucken –

Antwort

0

bitte ich überprüfen f Die Versionen sind identisch, wenn Sie einen anderen Google-Dienst wie diesen verwenden. Blick auf Datei build.gradle (Modul: 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' 

Überprüfen Sie, ob die Manifest-Datei zu ähnlich sind diese:

 ...<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> 

Verwandte Themen