Android Firebase通知消息已收到,但未显示通知提示? [英] Android Firebase Notification message received but notification prompt is not showing?

查看:566
本文介绍了Android Firebase通知消息已收到,但未显示通知提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照此教程进行操作,并且在尝试发送时从Firebase控制台收到一条消息,调用onMessageReceived并执行createNotification,没有通知提示出现.

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

下面是我的MyAndroidFirebaseMsgService代码

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

推荐答案

最后发现该通知实际上仅出现在android下拉菜单中,而不是由于我的设备运行的是Android 4而弹出. 5设备,并且通知会像本教程一样弹出.

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.

这篇关于Android Firebase通知消息已收到,但未显示通知提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆