【问题标题】:How to prevent intent opening an activity automatically upon receiving push notification如何防止意图在收到推送通知后自动打开活动
【发布时间】: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


    【解决方案1】:

    使用此代码生成通知

    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);
    

    【讨论】:

    • 我生成通知的代码适用于 Lollipop 及其后代的所有版本,但您的代码与所有版本不兼容。
    • 通知区域不会显示3-4行的大文本信息。它只会剪切文本并显示 1 行消息
    • 如果消息很大,那么您可以使用 \n 。
    • 不会将文本分成两行。无论消息是短消息还是长消息或包含 \n,您的代码都只会显示 1 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多