2016-11-23 2 views
1

Ich benutze FCM in meinem Android-Projekt und funktioniert gut, wenn App läuft. Aber wenn die App getötet oder geschlossen wird onReceiveMessage wird nicht aufgerufen. Ich habe versucht, indem ich Nachricht nur unter Verwendung der Daten playload auch noch es schicke, funktioniert nicht. Gibt es irgendeine Lösung? Vielen Dank im VorausFCM Hintergrund Benachrichtigung Problem Android

+0

zeigen Bitte Code –

+0

Check dies. das gleiche habe ich implementiert https://www.simplifiedcoding.net/firebase-cloud-messaging-tutorial-android/ –

+0

Ist es Ihr Code oder zeigen Sie uns auf einen Online-Artikel, den Sie bezeichnet? –

Antwort

0

Versuchen Sie diesen Code wird es Ihr Problem lösen. MyFirebaseMessagingService.java

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) { 
    // If the application is in the foreground handle both data and notification messages here. 
    // 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. 

    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Title: " + remoteMessage.getNotification().getTitle()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
    sendNotification("", ""); 
} 
// [END receive_message] 

/** 
* Create and show a simple notification containing the received FCM message. 
* 
* @param messageBody FCM message body received. 
*/ 
private void sendNotification(String xyz, String abc) { 
    Intent intent = new Intent(this, MainActivity.class); 
    Bundle b = new Bundle(); 


     b.putString("key", xyz); 
     b.putString("key", abc); 

    intent.putExtras(b); 

    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) 
      .setContentTitle("title") 
      .setContentText("body") 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    boolean useSilhouette = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; 
    if (useSilhouette) { 
     try { 
      notificationBuilder.setSmallIcon(R.mipmap.ic_sillate); 
     } catch (Exception e) { 
      notificationBuilder.setSmallIcon(R.mipmap.ic_sillate); 
     } 
    } else { 
     notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 
    } 

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


    //Random id number for notification 
    Random random = new Random(); 
    int m = random.nextInt(9999 - 1000) + 1000; 

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

Get Werte über getIntent() in MainActivity und tun, was Sie tun wollen;)

+0

ich habe auf die gleiche Weise implementiert. Es funktioniert nicht, wenn die App geschlossen ist. –

+0

Bearbeiten Sie Ihre Frage und posten Sie Ihren Code. –

Verwandte Themen