0

Ich habe diese tutorial gefolgt und wenn ich versuche, eine Nachricht von der Firebase-Konsole zu senden, wurden onMessageReceived aufgerufen und createNotification wurden durchgeführt, keine Benachrichtigung erscheint.Android Firebase-Benachrichtigungsnachricht wird empfangen, aber die Benachrichtigung wird nicht angezeigt?

Es nehme an, dies zu veranlassen, aber es hat nicht

it suppose to show prompt like this

Unten ist mein MyAndroidFirebaseMsgService Code

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

import com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 

import java.util.HashMap; 
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService 
{ 
    private static final String TAG = "MyAndroidFCMService"; 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
    //Log data to Log Cat 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
    //create notification 
    createNotification(remoteMessage.getNotification().getBody()); 
    } 

    private void createNotification(String messageBody) 
    { 
    Intent intent = new Intent(this , ResultActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent resultIntent = PendingIntent.getActivity(this , 0, intent, PendingIntent.FLAG_ONE_SHOT); 

    Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.mipmap.ic_launcher) 
     .setContentTitle("Test Notification") 
     .setContentText(messageBody) 
     .setAutoCancel(false) 
     .setSound(notificationSoundURI) 
     .setContentIntent(resultIntent); 

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

    notificationManager.notify(0, mNotificationBuilder.build()); 
    } 
} 
+0

Veröffentlichen Sie Ihren 'MyAndroidFirebaseMsgService'-Code –

+0

Es wird passieren, wenn Sie App im Hintergrund ist. Die Annahme ist, dass, wenn es sichtbar ist, App bereits im Fokus ist und du alles, was du willst, dort und dann ausführen wirst. –

+0

ist es besser, Ihre benutzerdefinierte Benachrichtigung onMessageReceived zu verwenden oder prüfen Sie zuerst Protokoll, ob Benachrichtigung empfangen wird oder nicht mit Protokoll oder Debug –

Antwort

1

Schließlich fand, dass die Anmeldung nur auf dem Android erschienen tatsächlich wurden Drop-Down-Menü statt auftauchen, weil mein Gerät Android 4 läuft. Ich teste wieder mit Android 5-Gerät und die Benachrichtigung erscheint genau wie das Tutorial.

0
Maybe you were missing notification notify. 
Please try bellow code. it's working properly 

//This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(RemoteMessage remoteMessage) { 
     Intent resultIntent = new Intent(this, MenuBarActivity.class); 
     resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     HashMap<String, String> dataMap = new HashMap<String, String>(remoteMessage.getData()); 
     resultIntent.putExtra(Constant.KEY_MESSAGE_BODY, dataMap); 
     resultIntent.putExtra(Constant.KEY_IS_NOTIFICATION, true); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(getString(R.string.app_name)) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get(Constant.KEY_BODY))) 
       .setContentText(remoteMessage.getData().get(Constant.KEY_BODY)) 
       .setAutoCancel(true) 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) 
       .setSound(defaultSoundUri); 

//  if (!BaseActivity.isInForeground()) { 
     notificationBuilder.setContentIntent(pendingIntent); 
//  } 
     // Generate ID 
     long time = new Date().getTime(); 
     String tmpStr = String.valueOf(time); 
     String last4Str = tmpStr.substring(tmpStr.length() - 5); 
     int notificationId = Integer.valueOf(last4Str); 

     // Sets an ID for the notification 
//   int notificationId = Constant.NOTIFICATION_ID; 

     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     // Builds the notification and issues it. 
     mNotifyMgr.notify(notificationId, notificationBuilder.build()); 
    } 
+0

Ich habe Fehler von kann nicht lösen Symbol Konstante und Datum() – Eddi

+0

Ich habe es geschafft, die Konstante und Datum() Fehler zu beheben, aber sogar mit Ihrer Funktion wird die Benachrichtigung angezeigt werden nicht angezeigt. Alle Codes in Ihrer Funktion wurden vollständig ausgeführt. Ich kann die Benachrichtigung auch hören. – Eddi

Verwandte Themen