收到Android Firebase通知消息但未显示通知提示?

时间:2017-05-05 08:17:41

标签: android firebase notifications android-notifications firebase-cloud-messaging

我已按照此tutorial进行操作,当我尝试从Firebase控制台发送消息时,onMessageReceived被调用并且createNotification已执行,未显示任何通知提示。

它假设提示这一点,但它没有

it suppose to show prompt like this

以下是我的MyAndroidFirebaseMsgService代码

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

2 个答案:

答案 0 :(得分:1)

终于发现通知实际上只出现在Android下拉菜单中,而不是由于我的设备运行Android 4而弹出。我再次使用Android 5设备进行测试,并且弹出通知就像教程一样。

答案 1 :(得分: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());
    }
相关问题