简体   繁体   中英

How to send repeating notifications properly on Android

I'm setting a repeat notification on Android but the Alarm doesnt't appear on my device.

I have read the Android Developers documentation and according to it my code seems just fine, but still it doesn't run properly. I am using a class as the BroadcastReceiver that receives the intent from the MainActivity and then it passes it to an IntentService .

This is the method that triggers the Alarm on the MainActivity, it runs every time the app is initialized.

 public void setAlarm(){
            alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(  MainActivity.this, 0, alarmIntent, 0);

            Calendar alarmStartTime = Calendar.getInstance();
            alarmStartTime.set(Calendar.HOUR_OF_DAY, 16);
            alarmStartTime.set(Calendar.MINUTE, 36);
            alarmStartTime.set(Calendar.SECOND, 0);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
            Log.i(TAG,"Alarms set every day.");

        }

This is the class that uses the BroadcastReceiver

public class AlarmReceiver extends BroadcastReceiver {

    private static final String TAG = "FOCUSALARM";


    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "BroadcastReceiver has received alarm intent.");
        Intent service1 = new Intent(context, AlarmService.class);
        context.startService(service1);

    }

}

And this is the class that uses the IntentService

public class AlarmService extends IntentService
{

    private static final int NOTIFICATION_ID = 101;
    private static final String TAG = "FOCUSALARM";


    public AlarmService() {
        super("AlarmService");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // don't notify if they've played in last 24 hr
        Log.i(TAG,"Alarm Service has started.");
        Context context = this.getApplicationContext();


        Intent mIntent = new Intent(this, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("test", "test");
        mIntent.putExtras(bundle);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Resources res = this.getResources();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "Focus");

        builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_launcher)
               // .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                .setTicker(res.getString(R.string.notification_title))
                .setAutoCancel(true)
                .setContentTitle(res.getString(R.string.notification_title))
                .setContentText(res.getString(R.string.notification_subject));


        NotificationManagerCompat notificationManager =  NotificationManagerCompat.from(this);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
        Log.i(TAG,"Notifications sent.");


    }

When I run it it shows all the logs, but the notification is not appearing on the device.

I/FOCUSALARM: Alarms set every day.
I/FOCUSALARM: BroadcastReceiver has received alarm intent.
I/FOCUSALARM: Alarm Service has started.
I/FOCUSALARM: Notifications sent.

As our appreciated contributor @RobCo said: in Oreo+ devices you have to create a notification channel, for this use the following method as shown in the Android Developers documentation:

 private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Hola";
            String description = "Focus bro";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
             notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

Then just use this same notificationManager object on your notificationBuilder

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);

        builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_launcher)
               // .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
               // .setTicker(res.getString(R.string.notification_title))
                .setAutoCancel(true)
                .setContentTitle(res.getString(R.string.notification_title))
                .setContentText(res.getString(R.string.notification_subject));


        notificationManager.notify(NOTIFICATION_ID, builder.build());
        Log.i(TAG,"Notifications sent.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM