在 Android 8.1 API 27 上不显示通知 [英] On Android 8.1 API 27 notification does not display

查看:30
本文介绍了在 Android 8.1 API 27 上不显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 8.1 API 27 上获得 Toast:

I get Toast on Android 8.1 API 27:

my_package_name"包的开发者警告
未能在...上发布通知

Developer warning for package "my_package_name"
Failed to post notification on ...

Logcat 包含下一个字符串:

Logcat includes next strings:

通知:不推荐将流类型用于其他操作比音量控制

Notification: Use of stream types is deprecated for operations other than volume control

W/Notification:请参阅 setSound() 的文档以了解要使用的内容而是使用 android.media.AudioAttributes 来限定您的播放用例

W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

E/NotificationService:没有找到 pkg=my_package_name 的频道

E/NotificationService: No Channel found for pkg=my_package_name

Toast 和 Logcat 中的完整信息可以帮助定位此问题.

The full information in the Toast and in Logcat can help in the localization this problem.

推荐答案

如果出现这个错误需要注意 2 个项目并按顺序排列:

If you get this error should be paid attention to 2 items and them order:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, important);
  2. builder = new NotificationCompat.Builder(context, id);

同样 NotificationManager notifManager 和 NotificationChannel mChannel 只创建一次.

Also NotificationManager notifManager and NotificationChannel mChannel are created only once.

通知需要设置器:

  • builder.setContentTitle()//必需
  • .setSmallIcon()//必需
  • .setContentText()//必需

参见示例:

private NotificationManager notifManager;
public void createNotification(String aMessage, Context context) {
    final int NOTIFY_ID = 0; // ID of notification
    String id = context.getString(R.string.default_notification_channel_id); // default_channel_id
    String title = context.getString(R.string.default_notification_channel_title); // Default Channel
    Intent intent;
    PendingIntent pendingIntent;
    NotificationCompat.Builder builder;
    if (notifManager == null) {
        notifManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = notifManager.getNotificationChannel(id);
        if (mChannel == null) {
            mChannel = new NotificationChannel(id, title, importance);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notifManager.createNotificationChannel(mChannel);
        }
        builder = new NotificationCompat.Builder(context, id);
        intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        builder.setContentTitle(aMessage)                            // required
               .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
               .setContentText(context.getString(R.string.app_name)) // required
               .setDefaults(Notification.DEFAULT_ALL)
               .setAutoCancel(true)
               .setContentIntent(pendingIntent)
               .setTicker(aMessage)
               .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    }
    else {
        builder = new NotificationCompat.Builder(context, id);
        intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        builder.setContentTitle(aMessage)                            // required
               .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
               .setContentText(context.getString(R.string.app_name)) // required
               .setDefaults(Notification.DEFAULT_ALL)
               .setAutoCancel(true)
               .setContentIntent(pendingIntent)
               .setTicker(aMessage)
               .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
               .setPriority(Notification.PRIORITY_HIGH);
    }
    Notification notification = builder.build();
    notifManager.notify(NOTIFY_ID, notification);
}

这篇关于在 Android 8.1 API 27 上不显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆