计划的通知适用于SDK 28,但不适用于SDK 23

时间:2019-10-11 19:11:17

标签: java android android-notifications

例如,我的代码在装有Android 8或更高版本的手机上可以正常工作,但是具有Android 6(我支持的最低版本)的手机不会收到任何通知。我需要处理SDK 23的通知吗?

发送通知的代码。

public static void sendNotification(Context context,Class<?> cls) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "CH1";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "WeeklyNotification",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("Test");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.MAGENTA);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationManager.createNotificationChannel(notificationChannel);
        }
        Intent notificationIntent = new Intent(context, cls);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(cls);
        stackBuilder.addNextIntent(notificationIntent);

        PendingIntent pendingIntent = stackBuilder.getPendingIntent(DAILY_CODE, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID);

        Notification notification = builder.setContentTitle("TestTitle")
                .setContentText("myContent")
                .setTicker("{myTicker}")
                .setColor(context.getResources().getColor(R.color.colorAccent))
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_logo)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentInfo("Info")
                .setContentIntent(pendingIntent).build();


        notificationManager.notify(DAILY_CODE, notification);

    }

我的AlarmReciever:

public class AlarmReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    //Trigger the notification
    NotificationService.sendNotification(context, MainActivity.class);
 }

以及用于设置时间表的代码部分

Intent intent1 = new Intent(context, cls);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

0 个答案:

没有答案