状态栏通知自动启动活动

Posted

技术标签:

【中文标题】状态栏通知自动启动活动【英文标题】:Status bar notification auto starts activity 【发布时间】:2015-04-05 19:01:20 【问题描述】:

我正在尝试进行预定通知。所有作品除了:当应用程序处于活动状态并最小化时。通知自动启动活动,无需等待用户点击。

回复:

 public void onReceive(Context context, Intent paramIntent) 

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        Notification notification = new Notification(R.drawable.logo_f, context.getResources().getString(R.string.notification_text), System.currentTimeMillis());

        Intent notificationIntent = new Intent(context, TimeLeftActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(context, context.getResources().getString(R.string.notification_text), "", intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.sound=alarmSound;
        // Fire the notification
        notificationManager.notify(1, notification);


    

我的通知启动方式:

 private void createScheduledNotification(int sec)
    
        // Get new calendar object and set the date to now
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        // Add defined amount of days to the date
        calendar.add(Calendar.SECOND, sec);

        // Retrieve alarm manager from the system
        AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);

        // Every scheduled intent needs a different ID, else it is just executed once
        int id = 1;

        // Prepare the intent which should be launched at the date
        Intent intent = new Intent(this, TimeAlarm.class);

        // Prepare the pending intent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(pendingIntent);
        // Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    

在基里尔回答后编辑。错误仍然存​​在。通知自动启动挂起的意图并且不等待点击。

  @Override
    public void onReceive(Context context, Intent paramIntent) 

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();


        Intent notificationIntent = new Intent(context, TimeLeftActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle(context.getResources().getString(R.string.notification_text))
                .setContentIntent(intent)
                .setSound(alarmSound)
                .build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Fire the notification
        notificationManager.notify(1, notification);


    

【问题讨论】:

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 零呢?第二个 0 应该是 PendingIntent 的常量 【参考方案1】:

很难发现错误,因为您在代码中使用了已弃用的 API,您应该使用Notication.Builder

 Notification noti = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

如果需要支持旧版本可以使用NotificationCompat

更新

这是我的应用程序的示例,它会抛出一个通知,通过点击打开活动,我标记了添加意图的方法。

    String message = context.getString(R.string.notif_message);
    Intent notificationIntent = new Intent(AddBpRecordActivity.ADD_ACTION);

    NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notif_logo)
            .setContentTitle(message)
            .setContentText(billet.comment)
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true)
      >>>   .setContentIntent(PendingIntent.getActivity(context, (int) billet.id, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT))
            .setWhen(System.currentTimeMillis());
    Notification notification = nb.build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify((int) billet.id, notification);

【讨论】:

如何设置意图?所以在舔它会打开活动吗?设置内容意图? 仍然自动打开活动,可能是手机故障?

以上是关于状态栏通知自动启动活动的主要内容,如果未能解决你的问题,请参考以下文章

如何删除在android中更改的自动状态栏颜色

从 android 推送通知单击启动时,意图数据未在活动中更新

Android状态栏通知:使其无法清除并返回应用程序(不启动新实例)

C# winForm启动最小化到任务栏右侧通知栏并交互操作

C# winForm启动最小化到任务栏右侧通知栏并交互操作

Android nougat 状态栏在启动活动时显示白色