Android8.0以后的的通知要指定channelId

@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    channel.setShowBadge(true);
    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}

public void sendChatMsg(String title, String content) {
    NotificationManager manager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(getActivity(), channelId)
            .setContentTitle(title)
            .setContentText(content)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.app_logo)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_logo))
            .setAutoCancel(true)
            .build();
    manager.notify(notifyId++, notification);
}

猜你喜欢

转载自blog.csdn.net/qq_27248989/article/details/123252008