我向用户发送推送通知,点击它时会打开应用程序.
我的问题是,当应用程序已经打开时,单击通知会再次启动应用程序.
我只希望它启动应用程序,如果它还没有运行.
我在通知中使用Pending Intent:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Splash.class), 0);
Run Code Online (Sandbox Code Playgroud)
我看到帖子说使用:
<activity
android:name=".Splash"
android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)
但问题是我正在运行的应用程序正在运行其他活动,然后启动应用程序启动后7秒内完成,因此当应用程序运行时,Splash不是当前活动
notifications android push-notification android-pendingintent
我一直在Android开发者网站上阅读以下文本,特别是在框架主题 - >服务 - >启动服务下.
在那里它陈述如下:
如果服务不提供绑定,则使用startService()提供的意图是应用程序组件与服务之间唯一的通信方式.但是,如果您希望服务返回结果,则启动服务的客户端可以为广播创建PendingIntent(使用getBroadcast())并将其传递给启动服务的Intent中的服务.然后,该服务可以使用广播来传递结果.
我有几个问题:
Services 和 IntentService s?Service; 然后,该服务可以使用广播来传递结果.以及所提到的广播将结果传递给原始客户/活动的位置?是否有一些方法应该被覆盖(比如onActivityResult())或什么?只要我的目标更新SDK至30(安卓R),棉绒警告Missing PendingIntent mutability flag出现在我的PendingIntent.FLAG_UPDATE_CURRENT标志时,我想定义PendingIntent。
我应该如何处理这个 lint 而不影响应用程序功能?
可能重复:
什么是待定意图?
我对意图和未决意图感到困惑.
任何人都可以解释两者之间究竟有什么区别?我搜索了SO,我找到了这个链接,但它不符合我的需要:https://stackoverflow.com/questions/5633810/what-is-difference-between-an-intent-and-a-pending - 意图.
我知道待定意图是一个意图,将在稍后开始.正常意图在传递给startActivity(intent)或时开始StartService(intent).
真的不确定如何搜索这个......
每当在我的队列中添加或删除作业以在状态栏中发出通知时,我都会调用以下内容:
private void showNotification()
{
int jobsize = mJobQueue.size();
int icon = (jobsize == 0) ?
android.R.drawable.stat_sys_upload_done :
android.R.drawable.stat_sys_upload;
Notification notification =
new Notification(icon, "Test", System.currentTimeMillis());
Intent intent = new Intent(this, FileManagerActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.flags =
(Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(this,
"Uploading to our servers",
getString((jobsize > 0) ?
R.string.notification_active_transfers :
R.string.notification_no_transfers),
pendingIntent);
mNotifyManager.notify(NOTIFICATION, notification);
}
Run Code Online (Sandbox Code Playgroud)
现在的行为是这样的:
如果用户注销并点击通知,它仍然会打开一个新的FileManagerActivity(操作!)我可以通过启动我的身份验证活动并以自然顺序将意图传递给我的堆栈来解决这个问题,当应用程序已经是跑步是我遇到困难的地方.
如果用户已经打开FileManagerActivity,则单击该通知将在其上放置第二个实例.在这种情况下,我希望当前运行的FileManagerActivity接收焦点而不是启动新实例.
我怎么能得到正确的行为?
notifications android android-activity android-pendingintent
我遇到了一个问题,alarmManager并且还有与其相关的额外内容.
如果我设置多个警报,它们将会关闭,但附加功能保持不变.
我已经读过这些问题:
我试过了:
一切都无济于事.我不知道为什么它不起作用.
这是一段代码:
Intent intent = new Intent(con,
AppointmentNotificationReciever.class);
intent.putExtra("foo", bar.toString());
int id = randomNum;
PendingIntent sender = PendingIntent.getBroadcast(con, id,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, scheduleExecution, sender);
Run Code Online (Sandbox Code Playgroud) android alarmmanager extras android-intent android-pendingintent
我有一个alarmManager用于在特定时间向用户发送通知的内容.由于有多个警报,我有多个未决的意图,我正在创建并提供一个唯一的ID,但在某些情况下,我需要获取所有待处理的意图,然后取消它们,这样我就可以重置警报.我试过这样做,我似乎仍然无法做到这一点所以我有几个问题:
这是你如何正确获取和取消PendingIntent?
Intent intent = new Intent(con, AppointmentNotificationReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(con, id, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
am.cancel(sender);
Run Code Online (Sandbox Code Playgroud)
意图是否需要与原始待定意图(附加内容和所有内容)完全匹配?
PendingIntent标志是否需要与原始待定意图的标志相匹配?
我在GCMIntentservice中编写了一个代码,用于向许多用户发送推送通知.我使用NotificationManager,在单击通知时将调用DescriptionActivity类.我还将event_id格式的GCMIntentService发送到DescriptionActivity
protected void onMessage(Context ctx, Intent intent) {
message = intent.getStringExtra("message");
String tempmsg=message;
if(message.contains("You"))
{
String temparray[]=tempmsg.split("=");
event_id=temparray[1];
}
nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(this, DescriptionActivity.class);
Log.i("the event id in the service is",event_id+"");
intent.putExtra("event_id", event_id);
intent.putExtra("gcmevent",true);
PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0);
String title="Event Notifier";
Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis());
n.setLatestEventInfo(this, title, message, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueID,n);
sendGCMIntent(ctx, message);
}
Run Code Online (Sandbox Code Playgroud)
这里我在上面的方法中得到的event_id是正确的,即我总是得到更新的.但是在下面的代码中(DescriptionActivity.java):
intent = getIntent();
final Bundle b = intent.getExtras();
event_id = Integer.parseInt(b.getString("event_id"));
Run Code Online (Sandbox Code Playgroud)
这里的event_id总是"5".无论我把什么放在GCMIntentService类中,我得到的event_id总是5.有人可以指出问题吗?是因为未决意图?如果是,那我该怎么处理呢?
push-notification android-notifications android-notification-bar android-pendingintent
我正在开发一个向用户显示通知的应用程序.通知的目的是使用户在另一个活动中轻松返回活动.我在我的应用程序中使用此代码来创建和显示通知.
notification = new Notification(R.drawable.icon,
"Notify",
System.currentTimeMillis());
notification.setLatestEventInfo(this, "App name",
"App message",
PendingIntent.getActivity(
this, 0,
new Intent(this, Main.class),
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_ONGOING_EVENT;
nManager.notify(0, notification);
Run Code Online (Sandbox Code Playgroud)
但是当用户点击通知时,会启动相同活动的新实例,而不是之前用户使用的实例.
我认为这与PendingIntent有关,但是我找不到如何使Intent恢复以前暂停的活动实例而不是创建新实例.
谢谢.
notifications android android-activity android-pendingintent
PendingIntent.FLAG_NO_CREATE的文档内容如下:
指示如果描述的PendingIntent尚不存在的标志,则只返回null而不是创建它.
我的问题:用什么标准来比较PendingIntents?
我猜这个标志使用了PendingIntent.equals,但是我不确定该函数使用的是什么标准.它是使用动作,requestCode,类别,额外(我猜不是)等?
语境:
如果我的警报尚未设置,我想以待定意图启动警报.具体来说,我正在听这个答案.
Intent i = new Intent(applicationContext, MyService.class);
i.setAction("myAction");
PendingIntent pi = PendingIntent.getService(applicationContext, /*requestCode*/0, i, PendingIntent.FLAG_NO_CREATE);
if (pi != null) {
AlarmManager alarmMgr = (AlarmManager)applicationContext.getSystemService(Context.AlarmService);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_HOUR, AlarmManager.INTERVAL_HOUR, pi);
}
Run Code Online (Sandbox Code Playgroud)