Android Firebase Notification message received but notification prompt is not showing?

无人久伴 提交于 2019-12-12 04:05:37

问题


I have followed this tutorial and when I try to send a message from Firebase console, onMessageReceived were called and createNotification were performed, no notification prompt shows up.

It suppose to prompt this but it didn't

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

回答1:


Finally found that the notification were actually only appeared on the android drop down menu instead of popping up due to my device is running Android 4. I test again with Android 5 device and the notification pop up just like the tutorial.




回答2:


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


来源:https://stackoverflow.com/questions/43799905/android-firebase-notification-message-received-but-notification-prompt-is-not-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!