如何防止在收到推送通知后自动打开活动

时间:2015-06-03 09:56:23

标签: android android-intent android-activity push-notification

我遇到一个奇怪的问题,即只要我在设备中收到Push NotificationActivity就会自动打开。我不希望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());
        }

    }

1 个答案:

答案 0 :(得分:0)

使用此代码生成通知

private static void generateNotification(Context context ,String name,String number) {
  int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Intent notificationIntent = null;
    @SuppressWarnings("deprecation")
    Notification notification = new Notification(icon, "", when);
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationIntent = new Intent(Intent.ACTION_DIAL);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    String p = "tel:" + number.trim();
    notificationIntent.setData(Uri.parse(p));
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(context, "uPosi", "Callback Reminder from "+name, intent);
   // notification.flags |= Notification.FLAG_AUTO_CANCEL;  
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/notification");
    notification.defaults |= Notification.DEFAULT_VIBRATE;        

    notificationManager.notify(generateUniqueId(), notification);