Android中的本地通知?
在iOS中,应用程序在后台使用“本地通知”,通知用户发生了一些事情,用户可能需要注意: 本地通知。。。在应用程序有新数据可用时通知用户,即使应用程序未在前台运行。例如,消息传递应用程序可能会让用户知道新消息何时到达,日历应用程序可能会通知用户即将到来的约会 [如果应用程序本身正在提供新数据,则为“本地”;如果远程服务器正在发送更新,则为“远程”。]Android中的本地通知?,android,notifications,local,Android,Notifications,Local,在iOS中,应用程序在后台使用“本地通知”,通知用户发生了一些事情,用户可能需要注意: 本地通知。。。在应用程序有新数据可用时通知用户,即使应用程序未在前台运行。例如,消息传递应用程序可能会让用户知道新消息何时到达,日历应用程序可能会通知用户即将到来的约会 [如果应用程序本身正在提供新数据,则为“本地”;如果远程服务器正在发送更新,则为“远程”。] Android上是否有类似的解决方案?LocalBroadcastManager看起来是一个更好的解决方案:创建您自己的自定义意图操作,将其广播到
Android上是否有类似的解决方案?LocalBroadcastManager看起来是一个更好的解决方案:创建您自己的自定义意图操作,将其广播到您的流程,并确保任何活动等都注册为该意图的接收者。如果您也针对旧API,请使用NotificationCompat.Builder
Intent intent = new Intent(ctx, HomeActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
如果您希望使用大数据触发本地通知,即在单个通知中使用多行文本,并带有标题、股票代码、图标和声音。。使用以下代码。。我想这会对你有帮助。
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());
Intent notificationIntent=新的意图(上下文、,
提醒阻止活动(类);
notificationIntent.putExtra(“点击”、“通知点击”);
notificationIntent.addFlags(Intent.FLAG\u活动\u清除\u顶部
|意图。标记活动(单个顶部);//只打开一个活动
//调用默认通知服务
通知经理通知经理;
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(
上下文);
Uri=铃声管理器
.getDefaultUri(RingtoneManager.TYPE_通知);
mBuilder.setContentTitle(“提醒”);
setContentText(“您有新的提醒。”);
setTicker(“新提醒警报!”);
mBuilder.setSmallIcon(R.可绘制时钟);
mBuilder.setSound(uri);
mBuilder.setAutoCancel(真);
//添加特定于大视图的配置
NotificationCompat.InboxStyle InboxStyle=新NotificationCompat.InboxStyle();
字符串[]事件=null;
事件[0]=新字符串(“您的第一行文本”);
事件[1]=新字符串(“您的第二行文本”);
//设置收件箱样式大视图的标题
setBigContentTitle(“您有提醒:”);
//将事件移动到大视图中
for(int i=0;i
很好的工作示例。我遵循了其他的示例代码,它与棒棒糖或更高版本一起工作。但这段代码也适用于kitkat(我不知道为什么…可能是图标还是默认值?)谢谢我的理解是:iPhone本地通知,是应用程序与用户之间的通信。Android上的LocalBroadcastManager是从应用程序的一部分到应用程序另一部分的通信。我认为LocalBroadcastManager与这个问题无关,但如果我错了,请纠正我。这似乎是这个问题中最古老的一个问题;它链接到该问题的至少一个更高版本,并且该问题的变体至少被问了4次。因此,我重写了它,使它不那么模糊,并投票重新开放它。依我看,这是一个基本的移动功能;这个问题值得提出和回答。[或者,如果有更好的答案,可以将其作为后续版本的副本关闭,因为此处不接受任何答案。谷歌“stackoverflow android local notification”]在其他线程上有两个有用的答案:,-从中我们可以看出,安卓没有与iOS功能“local notification”直接等效的功能。在解决方案中可能涉及到几种不同的Android API。因此,我理解,如果共识是这仍然是“太广泛”,尽管我个人希望看到,聚集在一个地方,可能的替代方案是“应用程序在后台,我们应该如何告诉用户发生了重要的事情?”