Cho*_* M. 101 notifications android
我需要一个在android上添加通知的程序.当有人点击通知时,它应该引导他们进行第二次活动.
我已经建立了一个代码.通知应该有效,但由于某种原因它无法正常工作.在Notification没有显示的.不知道我错过了什么.
这些文件的代码:
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject")
.setContentIntent(pIntent).setAutoCancel(true)
.setStyle(new Notification.BigTextStyle().bigText(longText))
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after it's selected
notificationManager.notify(0, n);
Run Code Online (Sandbox Code Playgroud)
Cho*_* M. 368
没有图标,代码将无法运行.因此,将setSmallIcon调用添加到构建器链中,以使其工作:
.setSmallIcon(R.drawable.icon)
Run Code Online (Sandbox Code Playgroud)
Android 8引入了一个channelId使用a 设置属性的新要求NotificationChannel.
private NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(verseurl);
bigText.setBigContentTitle("Today's Bible Verse");
bigText.setSummaryText("Text in detail");
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
mBuilder.setContentTitle("Your Title");
mBuilder.setContentText("Your text");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);
mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// === Removed some obsoletes
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
Android.App.NotificationImportance.Default);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
mNotificationManager.notify(0, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
sli*_*n77 57
实际上@ tato469的答案似乎不正确.然后,你的问题过于含糊,因为你没有提到错误或不起作用.
看看你的代码,我假设Notification根本没有显示.
您的通知未显示,因为您没有提供图标.即使SDK文档没有提到它是必需的,事实上它实际上是如此,如果没有它,你Notification将无法展示.
addAction仅自4.1起可用.在此之前,您将使用它PendingIntent来启动Activity.你似乎指定了一个PendingIntent,所以你的问题在于其他地方.从逻辑上讲,必须得出结论,这是缺少的图标.
小智 31
你错过了小图标.我犯了同样的错误,上面的步骤解决了它.
根据官方文档:Notification对象必须包含以下内容:
一个小图标,由setSmallIcon()设置
标题,由setContentTitle()设置
详细文本,由setContentText()设置
在Android 8.0(API级别26)及更高版本上,有效的通知通道ID,由setChannelId()设置或在创建通道时在NotificationCompat.Builder构造函数中提供.
请参阅http://developer.android.com/guide/topics/ui/notifiers/notifications.html
这让我今天感到震惊,但是我意识到这是因为在Android 9.0(Pie)上,默认情况下“ 请勿打扰”还会隐藏所有通知,而不是像在Android 8.1(Oreo)和之前那样仅使它们静音。这不适用于通知。
我喜欢为我的开发设备启用DND,因此进入DND设置并更改设置以仅使通知静音(但不隐藏它们)为我解决了该问题。
对于Android 8.1(Oreo)之后的Android版本,必须强制创建通知通道,以使通知可见。如果在您的应用程序中看不到针对Oreo + Android的通知,则需要在应用程序启动时调用以下函数-
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name,
importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviours after this
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73419 次 |
| 最近记录: |