【发布时间】:2015-06-03 09:56:23
【问题描述】:
我遇到了一个奇怪的问题,即每当我在设备中收到Push Notification 时,Activity 就会自动打开。我不希望 Activity 自动显示。我想要的是当用户点击通知时手动打开Activity。
这是我收到Push Notification后执行的方法:-
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("message");
// notifies user
generateSupportNotification(context, message);
}
这是收到推送通知时将调用的方法
private void generateSupportNotification(Context context, String message) {
String classString = "com.pkgName.MainActivity"
Intent notificationIntent = new Intent();
notificationIntent.setClassName(context, classString);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final boolean isKitKat = Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT;
if (isKitKat) {
NotificationCompat.Builder notification = new NotificationCompat.Builder(
getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ResHotel")
// .setContentText("Welcome!!!")
.setContentIntent(resultPendingIntent)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setFullScreenIntent(resultPendingIntent, true)
.setAutoCancel(false);
boolean isVibrate = new DevicePreferences().getBoolean(context,
Constants.VIBRATE, true);
boolean isSound = new DevicePreferences().getBoolean(context,
Constants.SOUND, true);
if (isVibrate && isSound) {
notification.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
} else if (isVibrate && !isSound) {
notification.setDefaults(Notification.DEFAULT_VIBRATE);
} else if (!isVibrate && isSound) {
notification.setDefaults(Notification.DEFAULT_VIBRATE);
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification.build());
} else {
NotificationCompat.Builder notification = new NotificationCompat.Builder(
getApplicationContext())
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ResHotel")
.setContentText(message)
.setContentIntent(resultPendingIntent)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setFullScreenIntent(resultPendingIntent, true)
.setAutoCancel(false);
// notification.setAutoCancel(true);
boolean isVibrate = new DevicePreferences().getBoolean(context,
Constants.VIBRATE, true);
boolean isSound = new DevicePreferences().getBoolean(context,
Constants.SOUND, true);
if (isVibrate && isSound) {
notification.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
} else if (isVibrate && !isSound) {
notification.setDefaults(Notification.DEFAULT_VIBRATE);
} else if (!isVibrate && isSound) {
notification.setDefaults(Notification.DEFAULT_VIBRATE);
}
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
}
}
【问题讨论】:
标签: android android-intent android-activity push-notification