How to show a notification everyday at a certain time even when the app is closed?

前端 未结 3 435
温柔的废话
温柔的废话 2021-02-04 10:52

Although this question might have been asked before on Stack Overflow, I still haven\'t found a clear answer.

I want to show a notification everyday at 12pm for example

3条回答
  •  [愿得一人]
    2021-02-04 11:19

    **After Working for a Longer time I want that everyOne should not Waste time so Kindly Approve My ANswer So it will be helpfull for Other developer** 
    
    1-  First Step Create a method which contains your Code where you will define your Time or at what time you want to show the notification.This method need to be called from where you want user to ask for notification.
    
     public void getNotification () {
    
    
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    
               Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
    
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
    
                alarmManager.cancel(pendingIntent);
    
                Calendar calendar = Calendar.getInstance();
                Calendar now = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 16);
                calendar.set(Calendar.MINUTE, 30);
                calendar.set(Calendar.SECOND, 00);
                if (now.after(calendar)) {
                    Log.d("Hey","Added a day");
                    calendar.add(Calendar.DATE, 1);
                }
    
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
            }
    
    
    2- Create a Notification_receiver class which is going to extend Broadcast Receiver here you are going to define your **Channel Id** as it is perfectly working for **API 25 and above** this the Notification_receiver class:
    
    
    
    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Build;
    import android.util.Log;
    
    import androidx.core.app.NotificationCompat;
    
    //Created By Prabhat Dwivedi
    public class Notification_receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder;
            PendingIntent pendingIntent;
    
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("Your App Name",
                        "You app Package Name",
                        NotificationManager.IMPORTANCE_HIGH);
                String channel_Id = channel.getId();
                CharSequence channel_name = channel.getName();
                Log.e("Notification_receiver", "channel_Id :" + channel_Id);
                Log.e("channel_name", "channel_name :" + channel_name);
    
                channel.setDescription("Make entry of today's spending now");
                notificationManager.createNotificationChannel(channel);
            }
    
            builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.yourapp_logo)
                    .setChannelId("Your app Name is your Channel Id")
                    .setContentTitle("Your title")
                    .setContentText("Your Description")
                    .setAutoCancel(true);
    
    //nder this you will find intent it is going to define after clicking notification which activity you want to redirect
            Intent repeatingIntent = new Intent(context, HomePage.class);
             pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent,    PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);
            notificationManager.notify(100, builder.build());
        }
        }
    

提交回复
热议问题