Android Oreo中的FCM推送通知问题
创始人
2024-08-14 22:00:55
0

在Android Oreo中,FCM(Firebase Cloud Messaging)推送通知需要做一些额外的适配工作。以下是一个解决Android Oreo中FCM推送通知问题的示例代码:

  1. 在AndroidManifest.xml文件中添加渠道声明:

  
    
  

  1. 在res/values/strings.xml文件中定义默认通知渠道ID:

    default_channel

  1. 创建一个NotificationHelper类来处理通知相关操作:
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 = "default_channel";
    private static final String CHANNEL_NAME = "Default Channel";
    private static final String CHANNEL_DESCRIPTION = "Default notification channel";

    public static void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(CHANNEL_DESCRIPTION);
            notificationManager.createNotificationChannel(channel);
        }
    }
}
  1. 在FirebaseMessagingService中调用NotificationHelper类的方法:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 处理收到的消息
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

    private void showNotification(String title, String body) {
        NotificationHelper.createNotificationChannel(this);

        // 创建通知
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationHelper.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());
    }
}

这样,在Android Oreo中,你就能够正确地显示FCM推送的通知了。请记得将代码中的图标和通知内容替换为你自己的内容。

相关内容

热门资讯

透视必备!wepoker辅助脚... 透视必备!wepoker辅助脚本,wepoker游戏的安装教程(透视)真是是真的挂(哔哩哔哩)1、不...
辅助透视!德普之星透视辅助软件... 辅助透视!德普之星透视辅助软件下载,微信小程序游戏辅助器,详细脚本(真的有挂)-哔哩哔哩1.德普之星...
透视教你!德普之星透视辅助,德... 透视教你!德普之星透视辅助,德普之星辅助工具如何打开(透视)其实是真的器(哔哩哔哩)1)有没有挂:进...
透视脚本!wepoker透视挂... 透视脚本!wepoker透视挂底牌,微乐自建房免费黑科技有哪些,专业脚本(发现有挂)-哔哩哔哩进入游...
脚本曝光!aapoker辅助器... 脚本曝光!aapoker辅助器怎么用,aapoker插件(透视)本来真的是有器(哔哩哔哩)1、点击下...
透视挂透视!约局吧是否有挂,微... 透视挂透视!约局吧是否有挂,微信小程序微乐辅助免费版,透视脚本(的确有挂)-哔哩哔哩1、让任何用户在...
透视规律!约局吧开挂神器是真的... 透视规律!约局吧开挂神器是真的吗,微乐智能辅助真的假的,揭幕脚本(发现有挂)-哔哩哔哩约局吧开挂神器...
透视辅助!wepoker怎么提... 透视辅助!wepoker怎么提高运气,wepoker辅助软件视频(透视)确实存在有器(哔哩哔哩)1、...
随着!hhpoker开挂教程,... 随着!hhpoker开挂教程,微信小程序辅助器脚本,辅助透视(确实有挂)-哔哩哔哩1、完成hhpok...
脚本专业!wepoker轻量版... 脚本专业!wepoker轻量版透视方法,wepoker脚本下载(透视)确实真的是有教程(哔哩哔哩)一...