并非在所有设备上都触发本地通知

时间:2018-09-01 12:47:35

标签: java android notifications calendar alarmmanager

我有一个本地通知,要在每天的特定时间重复。我尝试在少数设备上进行测试,但仅在少数设备上工作。请检查下面的代码并阐明一些内容。我尝试使用三星和LG设备。

下面是我的AlarmReceiver

 public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_NOTIFICATION_URI);
    mediaPlayer.start();


    NotificationManager manager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.logo)
            //example for large icon
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setContentTitle("Hey")
            .setContentText("Did you read today")
            .setOngoing(false)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true);
    Intent i = new Intent(context, VerseActivity.class);
    PendingIntent pendingIntent =
            PendingIntent.getActivity(
                    context,
                    0,
                    i,
                    PendingIntent.FLAG_ONE_SHOT
            );
    // example for blinking LED
    builder.setLights(0xFFb71c1c, 1000, 2000);
    builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    builder.setContentIntent(pendingIntent);
    manager.notify(12345, builder.build());
}
}

我的MainActivity

 public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    //Setting time of the day (8am here) when notification will be sent every day (default)
    calendar.set(Calendar.HOUR_OF_DAY,15);
    calendar.set(Calendar.MINUTE, 50);
    SetAlarm(calendar.getTimeInMillis());

    //alarm service

}

private void SetAlarm(long timeInMillis) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, broadcast);
}


}

Manifest.xml

    <receiver android:name=".AlarmReceiver"
        android:enabled="true"
        android:exported="true"
        />

2 个答案:

答案 0 :(得分:1)

在致电manager.notify(12345, builder.build());

之前创建一个通知渠道
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
    manager.createNotificationChannel(channel);
}
builder.setChannelId(CHANNEL_ID);
manager.notify(12345, builder.build());

答案 1 :(得分:0)

只是一个摘要,您必须涵盖所有这些构建版本。

  if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
            if (LOG_DEBUG) Log.v(TAG, " : version : <=M ");

        //noinspection deprecation
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_subtext))
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setLights(Color.CYAN, 500, 1200)
                .setPriority(Notification.PRIORITY_MAX)
                .setDeleteIntent(piDismiss)
                //.setContentIntent(pIpanel)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

        notification = builder.build();

        notification.contentView = collapsedView;
        notification.bigContentView = expandedView;

    } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N | Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
        if (LOG_DEBUG) Log.v(TAG, " : version : N| N1 - 24: 25 ");

        //noinspection deprecation
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_subtext))
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setLights(Color.CYAN, 500, 1200)
                .setPriority(Notification.PRIORITY_MAX)
                .setDeleteIntent(piDismiss)
                //.setContentIntent(pIpanel)
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

        notification = builder.build();


    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (LOG_DEBUG) Log.v(TAG, " : version : >=O ");


        NotificationChannel mChannel = new NotificationChannel
                (NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

        mChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.CYAN);
        mChannel.enableVibration(true);
        notificationManager.createNotificationChannel(mChannel);

        NotificationChannelGroup mGroup = new NotificationChannelGroup(NOTIFICATION_GROUP_ID, NOTIFICATION_GROUP_NAME);
        notificationManager.createNotificationChannelGroup(mGroup);

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

                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_subtext))
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true)
                .setDeleteIntent(piDismiss)
                //.setContentIntent(pIpanel)
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

        notification = builder.build();
    }

    if (notificationManager != null) {
        notificationManager.notify(NOTIFICATION_ID, notification);
    }