通知不适用于 API 23

Notification not working on API 23

我的通知不适用于 API 23.

我的通知从 API 16 到 22

成功

calstd/calmin 的 100 是默认数字。

 if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        if(calstd==100||calmin==100){
            cancelAlarm();
        }
        else {
            onTimeSet(calstd, calmin);
        }

        pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, mainsite.class), 0);
        nm1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notif = new Notification.Builder(context);
        uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        notif.setContentTitle("Remind Me");
        notif.setContentText("Vergiss deine Pille nicht :)");
        notif.setSmallIcon(R.drawable.ic_launcher);
        notif.setSound(uri);
        notif.setAutoCancel(true);
        notif.setContentIntent(pendingIntent);
        nm1.notify(15, notif.build());

...
}

 public void setAlarm(Calendar targetCal) {

    intent = new Intent(contexta, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(contexta, RQS_1, intent, 0);
    alarmManager = (AlarmManager) contexta.getSystemService(Context.ALARM_SERVICE);

    if (Build.VERSION.SDK_INT >= 19)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
    else if (Build.VERSION.SDK_INT >= 16)
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1 * 1000 * 60 * 60 * 24, pendingIntent);
}

清单:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:process=":remote" android:name=".Alarm"/>

  <receiver android:name="com.victoriaremindme.AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

关闭应用程序后无法使用。我找不到问题。

对于 API 23 及以上,您可以使用 setAndAllowWhileIdle()

所以将代码更改为

    if (Build.VERSION.SDK_INT >= 23)
            alarmManager. setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

   else if (Build.VERSION.SDK_INT >= 19)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

   else if (Build.VERSION.SDK_INT >= 16)
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1 * 1000 * 60 * 60 * 24, pendingIntent);

查看文档 here

希望对您有所帮助。

与23的区别是运行时间权限。您需要检查您的应用是否存在危险权限。由于新 api 意味着如果 运行 时间权限未得到管理,应用程序将不会 运行 很多任务,但会默默地忽略需要权限的代码,或者崩溃。

Requesting Permissions at Run Time

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

Normal and Dangerous Permissions

  • Normal permissions cover areas where your app needs to access data or resources outside the app's sandbox, but where there's very little risk to the user's privacy or the operation of other apps. For example, permission to set the time zone is a normal permission. If an app declares that it needs a normal permission, the system automatically grants the permission to the app. For a full listing of the current normal permissions, see Normal permissions.

  • Dangerous permissions cover areas where the app wants data or resources that involve the user's private information, or could potentially affect the user's stored data or the operation of other apps. For example, the ability to read the user's contacts is a dangerous permission. If an app declares that it needs a dangerous permission, the user has to explicitly grant the permission to the app.

Table 1. Dangerous permissions and permission groups.