安卓系统是否会阻止 FCM 通知?
创始人
2024-11-09 15:30:43
0

安卓系统不会主动阻止 FCM(Firebase Cloud Messaging)通知,但是在某些情况下,可能会出现通知被系统阻止的情况。

以下是一种解决方法的代码示例,可以通过设置通知通道(Notification Channel)来提高通知的可靠性,并避免被系统阻止:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

public class NotificationHelper {

    private static final String CHANNEL_ID = "FCM_CHANNEL_ID";
    private static final String CHANNEL_NAME = "FCM_CHANNEL_NAME";

    public static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

在你的应用程序的入口点(例如 Application 类的 onCreate 方法)中,调用 createNotificationChannel 方法来创建通知通道:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationHelper.createNotificationChannel(this);
    }
}

这样,当你发送 FCM 通知时,可以使用指定的通道 ID,以确保通知可以正常显示而不被系统阻止:

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification() != null) {
            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
    }

    private void showNotification(String title, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_notification);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

通过设置通知通道,可以确保 FCM 通知在安卓系统上的可靠性,并避免被系统阻止。

相关内容

热门资讯

透视攻略!哈糖大菠萝破解器,w... 透视攻略!哈糖大菠萝破解器,wpk俱乐部是真的吗,辅助教程(有挂细节)-哔哩哔哩1、这是跨平台的哈糖...
透视app!wepoker究竟... 透视app!wepoker究竟有没有透视,wejoker辅助软件,2025新版技巧(有挂分享)-哔哩...
透视挂!wpk辅助,wepok... 透视挂!wpk辅助,wepoker代打辅助机器人,曝光教程(的确有挂)-哔哩哔哩wepoker代打辅...
透视ai代打!hhpoker可... 透视ai代打!hhpoker可以控制牌吗,wepoker透视脚本是什么,AI教程(有挂规律)-哔哩哔...
透视神器!wpk私人局辅助是真... 透视神器!wpk私人局辅助是真的吗,德普之星私人局辅助免费,曝光教程(有挂实锤)-哔哩哔哩1、用户打...
透视计算!aapoker透视怎... 透视计算!aapoker透视怎么用,werplan怎么透视,技巧教程(有挂秘笈)-哔哩哔哩1、许多玩...
透视计算!德普之星辅助器,wp... 透视计算!德普之星辅助器,wpk插件,解密教程(有挂教学)-哔哩哔哩暗藏猫腻,小编详细说明德普之星辅...
透视讲解!wepoker亲友圈... 透视讲解!wepoker亲友圈有用吗,wpk可以作弊吗,AI教程(有挂透明挂)-哔哩哔哩1、很好的工...
透视代打!wepoker-h5... 透视代打!wepoker-h5下载,wpk可以作弊吗,分享教程(确实有挂)-哔哩哔哩1、玩家可以在w...
透视攻略!wepoker透视脚... 透视攻略!wepoker透视脚本是什么,hhpoker透视脚本下载,安装教程(有挂方法)-哔哩哔哩该...