Araz Mohammadnejad
Araz Mohammadnejad

Reputation: 41

Notification at Specific Time using BroadcastReceiver and AlarmManager

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>

Upvotes: 0

Views: 5559

Answers (3)

Aravind jayan
Aravind jayan

Reputation: 131

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());

}

}

Upvotes: 1

PaulT
PaulT

Reputation: 4296

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

Upvotes: 3

Araz Mohammadnejad
Araz Mohammadnejad

Reputation: 41

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)

Upvotes: 1

Related Questions