0

I want to send notification at specific time using broadcast receiver. many tutorials videos and also answers has been read in this regard and all of them was clear. but I couldn't find where is the problem of below codes because CODE still does not work. all of this was implemented based on "Daily Repeating Local Notification In Android - YouTube"

Here is the CODE within LAUNCHER Activity:

public class Splash extends Activity {

@RequiresApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    NotificationCheckPoint();

}

@RequiresApi(Build.VERSION_CODES.N)
private void NotificationCheckPoint() {

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 53);
    calendar.set(Calendar.SECOND, 0);

    Intent MyIntent = new Intent(getApplicationContext(), BroadastNotification.class);
    PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,
            MyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    MyAlarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, MyPendIntent);
       }
}

and this is the BroadcastReceiver CODE:

public class BroadastNotification extends BroadcastReceiver {

@Override
 public void onReceive(Context context, Intent intent) {
MyNotification(context);
}
 private void MyNotification(Context context) {
 String BigNotificqationText = "BigNotificqationText";
 String NotificationTitle = "NotificationTitle";
 String NotificationTicker = " NotificationTicker ";

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

 Intent MyIntent = new Intent(context, Splash.class);
 MyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

 PendingIntent MyPendingIntent = PendingIntent.getActivity(context, 100, MyIntent,
         PendingIntent.FLAG_UPDATE_CURRENT);

 NotificationCompat.Builder MyNB = new NotificationCompat.Builder(context);
 MyNB.setSmallIcon(R.drawable.icon);
 MyNB.setContentTitle(NotificationTitle);
 MyNB.setContentText(BigNotificqationText);
 MyNB.setTicker(NotificationTicker);
 MyNB.setPriority(NotificationCompat.PRIORITY_MAX);
 MyNB.setDefaults(NotificationCompat.DEFAULT_SOUND);
 MyNB.setAutoCancel(true);
 MyNB.setContentIntent(MyPendingIntent);

 Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
 MyNB.setLargeIcon(MyPicture);

 NotificationCompat.BigPictureStyle MyPicStyle = new NotificationCompat.BigPictureStyle().bigPicture(MyPicture);
 MyPicStyle.setSummaryText("Etude can makes our life Enlightened");
 MyNB.setStyle(MyPicStyle);

 NotificationCompat.BigTextStyle MyTextStyle = new NotificationCompat.BigTextStyle();

 MyTextStyle.bigText(BigNotificqationText);
 MyTextStyle.setBigContentTitle(NotificationTitle);
 MyNB.setStyle(MyTextStyle);


 MyNotifyManager.notify(100, MyNB.build());

 }

at the end it's the manifest definition:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ietude.etude">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="icon, label">

    <receiver android:name=".BroadastNotification">
        <!--android:enabled="true"-->
        <!--android:exported="true">-->
        <!--<intent-filter>-->
            <!--<action android:name="android.media.VOLUME_CHANGED_ACTION" />-->
        <!--</intent-filter>-->
    </receiver>

    <activity android:name=".Splash"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

3 Answers3

3

If you wish to have a timer expire at an exact time, then you can configure the timer as follows.

Calendar alarmFor = Calendar.getInstance();
alarmFor.set(Calendar.HOUR_OF_DAY, 7);
alarmFor.set(Calendar.MINUTE, 45);
alarmFor.set(Calendar.SECOND, 0);

Intent MyIntent = new Intent(getApplicationContext(), BroadcastNotification.class);
PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
MyAlarm.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmFor.getTimeInMillis(), MyPendIntent);

In this example, onReceive() is as follows:

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("MyAPP", "onReceive() called");
}

When I run this, the following is logged at the the exact time requested:

11-22 07:45:00.730 5833-5833/com.sap.myapplication D/MyAPP: onReceive() called
PaulT
  • 4,266
  • 2
  • 13
  • 15
  • really thanks dear Paul, I just copy and paste your CODE letter by letter and disabled extra codes related to my app, but I didn't receive your test result. I really confused. what's going on to my app. – Araz Mohammadnejad Nov 22 '17 at 19:37
  • the only difference between my code and yours is `@RequiresApi(Build.VERSION_CODES.N)`. do you think it is change my result?, when I use `Calendar` , it gives an error and needs Api – Araz Mohammadnejad Nov 22 '17 at 19:55
  • I did not need to use @RequiresApi and received no errors. Please check your imports: are you using android.icu.util.Calendar? Please note that in my example I used java.util.Calendar. – PaulT Nov 22 '17 at 20:41
  • It was solved finally, problem was that exactly, I import `java.util.Calendar` and everything became OK, really thanks for your kindly support dear Paul. – Araz Mohammadnejad Nov 22 '17 at 20:57
  • You are welcome. I am happy to hear it works for you now. – PaulT Nov 22 '17 at 21:10
  • Here is the latest answer, tested on Android 10. https://stackoverflow.com/a/67812298/9942927 – 4xMafole Jun 02 '21 at 21:04
1

The problem was detected, as I searched again in this regard I found that when Calendar has been set for 10 Second calendar.set(Calendar.SECOND, 10);it means BroadcastReceiver() must trigger Notification after 10 Second. it doesn't care the time of day. question still exist, how we can tell broadcastreceiver() to Show Notification based on the time of day that we defined in the CODE exactly? such as 16:33:24 (hh:mm:ss)

1

You can send notification at specific time daily by following method

Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.HOUR_OF_DAY, 12);
         calendar.set(Calendar.MINUTE, 0);
         calendar.set(Calendar.SECOND, 0);

Intent notifyIntent = new Intent(getApplicationContext(),showNotification.class);
       notifyIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(etApplicationContext(),0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  calendar.getTimeInMillis(),1000 * 60 * 60 * 24, pendingIntent);

Then in showNotification class

public class showNotification extends BroadcastReceiver {

public showNotification() {

}

@Override
public void onReceive(Context context, Intent intent) {
   sendNotification(context);
}

private void sendNotification(Context context) {


    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(context, SecondActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "101";

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);

        //Configure Notification Channel
        notificationChannel.setDescription("Game Notifications");
        notificationChannel.enableLights(true);
        notificationChannel.setVibrationPattern(new long[]{200});
        notificationChannel.enableVibration(false);

        notificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title.get("Title of notification"))
            .setContentText(subText.get("Sub text of notification"))
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX);


    notificationManager.notify(1, notificationBuilder.build());

}

}

Aravind jayan
  • 101
  • 1
  • 4