android - 在后台或应用程序关闭时未收到 Firebase 通知

标签 android firebase push-notification firebase-cloud-messaging

应用程序在关闭或后台时不会收到通知。下面是处理通知的我的服务类。 通知仅在唯一的应用程序运行时有效。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFMService";
    Random random = new Random();
    int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification();


    }

    private void sendNotification() {
        Intent intent = new Intent(this, NotificationDisplay.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_bouddha_icon)
                .setContentTitle("Bouddha Meridian School")
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }
}

最佳答案

你有没有下载 google-service.json 并放在 app 文件夹中? 遵循此示例以供引用:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

关于android - 在后台或应用程序关闭时未收到 Firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715621/

相关文章:

ios - 推送通知的自定义声音不起作用

android - 当应用程序位于前台时如何向通知托盘显示通知

android - 如何使用协程将数据从存储库传递到 ViewModel

java - 使用 float 变量在 Android 应用程序中显示板球比赛

node.js - Firebase Auth,在自定义 token 中,some-uid 应该是什么?

android - 使用 c2dm 一次发送多个推送

java - 无法在某些设备上未调用 Looper.prepare() 的线程内创建处理程序

firebase - 如何从 Firestore 导出安全和索引规则?

javascript - 至少需要 2 次刷新才能加载页面

node.js - Node JS - Web 推送通知