在Android 8.1 API 27上,通知不会显示

在Android 8.1 API 27上,通知不会显示,第1张

概述我在 Android 8.1 API 27上获得了Toast: Developer warning for package “my_package_name” Failed to post notification on … Logcat包含下一个字符串: Notification: Use of stream types is deprecated for operations other th 我在 Android 8.1 API 27上获得了Toast:

Developer warning for package “my_package_name”
Failed to post notification on …

Logcat包含下一个字符串:

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

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

E/NotificationService: No Channel found for pkg=my_package_name

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

解决方法 如果你得到这个错误应该注意2项并且他们订购:

> NotificationChannel mChannel = new NotificationChannel(ID,name,importance);
> builder = new NotificationCompat.Builder(context,ID);

notificationmanager notifManager和NotificationChannel mChannel也只创建一次.

通知需要设置者:

> 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});            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,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,400});    }    else {        builder = new NotificationCompat.Builder(context,400})               .setPriority(Notification.PRIORITY_HIGH);    }    Notification notification = builder.build();    notifManager.notify(NOTIFY_ID,notification);}
总结

以上是内存溢出为你收集整理的在Android 8.1 API 27上,通知不会显示全部内容,希望文章能够帮你解决在Android 8.1 API 27上,通知不会显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121472.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存