Android通知未显示
我需要一个可以在Android上添加通知的程序.并且当某人单击通知时,它应引导他们进行我的第二项活动.
I need a program that will add a notification on Android. And when someone clicks on the notification, it should lead them to my second activity.
我已经建立了代码.该通知应该可以正常工作,但是由于某种原因它不能正常工作. Notification
完全不显示.我不知道我在想什么.
I have established code. The notification should be working, but for some reason it is not working. The Notification
isn't showing at all. I don't know what am I missing.
这些文件的代码:
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);
没有图标,代码将无法正常工作.因此,将setSmallIcon
调用添加到构建器链,如下所示:
The code won't work without an icon. So, add the setSmallIcon
call to the builder chain like this for it to work:
.setSmallIcon(R.drawable.icon)
Android Oreo(8.0)及更高版本
Android 8引入了使用NotificationChannel
设置channelId
属性的新要求.
Android Oreo (8.0) and above
Android 8 introduced a new requirement of setting the channelId
property by using 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.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
mNotificationManager.notify(0, mBuilder.build());