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推送的通知了。请记得将代码中的图标和通知内容替换为你自己的内容。

相关内容

热门资讯

最新技巧!wepoke透明挂可... 最新技巧!wepoke透明挂可以识别,广东雀神麻雀提升胡牌几率,捞腌菜app开发有挂吗(有挂辅助);...
盘点一款!德扑之星数据解析,广... 盘点一款!德扑之星数据解析,广东雀神麻雀辅助器在哪里下载,福建天天开心辅助工具(有挂黑科技)1、不需...
技巧知识分享!菠萝德州偷偷看功... 技巧知识分享!菠萝德州偷偷看功能,雀神小程序辅助app下载,闲逸碰胡辅助神器ios(有挂脚本)1、闲...
重大通报!微扑克软件,雀神小程... 重大通报!微扑克软件,雀神小程序开通会员提高胜率,掌中乐游戏中心有辅助器吗(有挂脚本)1、实时掌中乐...
来一盘!聚星扑克有挂,广东雀神... 来一盘!聚星扑克有挂,广东雀神麻将插件ios怎么购买,jj斗地主自建房(有挂黑科技);1、下载好jj...
玩家必看科普!aapoker智... 玩家必看科普!aapoker智能ai辅助,雀神辅助器最新款,博乐温州棋牌有没有挂(有挂方法);1)博...
记者爆料!wpk数据工具,雀神... 记者爆料!wpk数据工具,雀神小程序是正规的吗,途游斗地主可以用挂吗(有挂黑科技);该软件可以轻松地...
分享开挂内幕!wepoke有辅... 分享开挂内幕!wepoke有辅助挂,微信麻将雀神最佳设置,中至九江游戏辅助器(有挂工具)1.中至九江...
玩家实测!wepoke真的,广... 玩家实测!wepoke真的,广东雀神智能插件安装如何使用,中至九江小程序辅助器(有挂方法)1、下载好...
玩家亲测!aapoke辅助工具... 玩家亲测!aapoke辅助工具存在,广东雀神辅助器怎么买,科乐麻将挂件神器通用(有挂方法)1、每一步...