локальное уведомление не срабатывает на всех устройствах - PullRequest
0 голосов
/ 01 сентября 2018

У меня локальное уведомление, которое я повторяю каждый день в определенное время. Я пробовал тестировать на нескольких устройствах, и это работает только на нескольких устройствах. Пожалуйста, проверьте код ниже и пролите немного света. Я пробовал с устройствами Samsung и 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());
}
}

И моя основная активность

 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 голосов
/ 01 сентября 2018

Просто фрагмент, вы должны охватить все эти версии сборки.

  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);
    }
0 голосов
/ 01 сентября 2018

Создать канал уведомлений перед вызовом 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());
...