0

Hello!!!

I try to make my app send notifications if isn't used for a day (or more time) but I'm not able to make it. I tried to implement it with code below but not worked. Any idea what I have to add to my code or how to make this possible?

ActivityMain.java

public class MainActivity extends AppCompatActivity {
    private static final int NOTIFICATION_REMINDER_DAY = 3;
...
protected void onCreate(Bundle savedInstanceState) {
...
        Intent notifyIntent = new Intent(this,NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast
                (context, NOTIFICATION_REMINDER_DAY, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  System.currentTimeMillis(),
                1000 * 60 * 60 * 24, pendingIntent);
       

NotificationIntentService.java

  protected void onHandleIntent(Intent intent) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("D4rK Cleaner!!!");
        builder.setContentText("You didn't cleaned your phone for some time. Try now!");
        builder.setSmallIcon(R.drawable.ic_splash_screen);
        Intent notifyIntent = new Intent(this, MainActivity.class);
        @SuppressLint("UnspecifiedImmutableFlag") PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //to be able to launch your activity from the notification
        builder.setContentIntent(pendingIntent);
        Notification notificationCompat = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);

NotificationReciver.java

  public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent(context, NotificationIntentService.class);
        context.startService(intent1);

Thanks in advance!

D4rK
  • 329
  • 4
  • 12
  • Can you specify what exactly didn't work? The alarm not fired? or alarm fired but notification was not displayed? Also it looks like you've missed notification channel. Please provide more details – Pavel B. Aug 09 '21 at 14:02
  • Nothing... Like... Notification doesn't appear. I have to add another timer or what? XD I followed a tutorial and a question [link](https://stackoverflow.com/questions/34517520/how-to-give-notifications-on-android-on-specific-time) – D4rK Aug 09 '21 at 14:14
  • 1
    Add log before sending the notification and set the alarm for a couple of minutes - just to see that your alarm works. If the alarm fires - the issue is this notification channel I've mentioned. Also the question link you referred is too old and outdated. – Pavel B. Aug 09 '21 at 14:21
  • Alright. Let me see if I'm able to check it (me beginner noob but I love to learn XD) By the way, thanks so much! – D4rK Aug 09 '21 at 20:02
  • Alright. Added value to check logs before notification inspired by [Android Developers](https://developer.android.com/studio/debug/am-logcat) and I saw is starting but not showing the notification. I guess you was true about I missed the notification channel but can you explain me more about it? ^-^ – D4rK Aug 09 '21 at 22:22
  • Yes, google has changed the way to create notifications since android 8. The new way is described here - you can mostly copy&paste from there https://developer.android.com/training/notify-user/build-notification – Pavel B. Aug 10 '21 at 10:14

0 Answers0