我正在使用推送通知。 当用户点击通知时我想启动应用程序以防它未启动,或者在启动时将其置于前面。 感谢
答案 0 :(得分:1)
实施pending intent
的代码强>
Intent pi = new Intent();
pi.setClass(getApplicationContext(), youactivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT);
String msgText = mMessage;
// construct the Notification object.
Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis());
清单
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity>
答案 1 :(得分:1)
这是我在这里找到答案的完整代码Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setContentTitle(extras.getString("title"))
.setContentText(extras.getString("message"))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, noti);
重要的是Intent上的标志,这将打开应用程序或者如果打开则将其带到前面,或者如果您在浏览应用程序时单击通知则不执行任何操作
答案 2 :(得分:0)
只需设置通知的意图,official API guide中详细介绍了这一点。
答案 3 :(得分:0)
您可以通过创建PendingIntent
来实现这一目标
例如,这将在单击通知时启动MainActivity。
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Notication title")
.setContentText("Content text")
.setContentIntent(pendingIntent).build();
答案 4 :(得分:0)
我不知道您是否在谈论Google Cloud Messaging。但如果这是正常的通知,那么在创建通知时,您需要提供Pending Intent。只需将您想要的类放在Pending intent中,这样当用户点击该通知时,您将被驱动到您所谓的应用程序。一个片段:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
"Your application activity".class), PendingIntent."Flag you want");
此后在创建通知时使用此意图。它的方法是setContentIntent(“above intent”)并使用NotificationManager激活你的通知!