在 Android 中发送本地通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11433632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Local Notifications in Android
提问by Mehroze Yaqoob
I want to use local notification in android for my application. If the app is not opened for 24 hours than a local notification is send. Can any one let me know how it should be done.
我想在 android 中为我的应用程序使用本地通知。如果应用程序在 24 小时内未打开,则会发送本地通知。任何人都可以让我知道应该如何做。
采纳答案by KrispyDonuts
See: Local Notifications in Android?You should be able to schedule an Intent with alarm manager every hour.
请参阅:Android 中的本地通知?您应该能够每小时使用警报管理器安排一次意图。
回答by Makvin
Intent intent = new Intent(context, yourActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("notification")
.setContentTitle("notification")
.setContentText("notification")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(pIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
回答by Pratibha Sarode
If you want to fire local notification with big data i.e., with multiline text in single notification with title, Ticker, icon, sound.. use following code.. I think it will help you..
如果您想使用大数据触发本地通知,即在带有标题、代码、图标、声音的单个通知中使用多行文本.. 使用以下代码.. 我认为它会帮助你..
Intent notificationIntent = new Intent(context,
ReminderListActivity.class);
notificationIntent.putExtra("clicked", "Notification Clicked");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity
// Invoking the default notification service
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Uri uri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setContentTitle("Reminder");
mBuilder.setContentText("You have new Reminders.");
mBuilder.setTicker("New Reminder Alert!");
mBuilder.setSmallIcon(R.drawable.clock);
mBuilder.setSound(uri);
mBuilder.setAutoCancel(true);
// Add Big View Specific Configuration
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = null;
events[0] = new String("Your first line text ");
events[1] = new String(" Your second line text");
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("You have Reminders:");
// Moves events into the big view
for (int i = 0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
mBuilder.setStyle(inboxStyle);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context,
ReminderListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(context);
stackBuilder.addParentStack(ReminderListActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// notificationID allows you to update the notification later on.
mNotificationManager.notify(999, mBuilder.build());