Search code examples
androidnotificationsalarmmanager

Set timed status bar notification


I want to activate a notification when I press a button, at certain hour for this example 12:56 so I used this in my code:

Button notificationButton = (Button) findViewById(R.id.button1);

    notificationButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(MainActivity.this , Notify("Title: Meeting with Business",
                   "Msg:Pittsburg 10:00 AM EST "));     
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       PendingIntent pendingIntent1 = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);

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

       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent1);

and i created a class named Notify which contains these codes :

@SuppressWarnings("deprecation")
    private Class<?> Notify(String notificationTitle, String    notificationMessage) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
           @SuppressWarnings("deprecation")
           Notification notification = new Notification(R.drawable.ic_launcher,
             "New Message", System.currentTimeMillis());
           notification.setLatestEventInfo(getBaseContext(),   notificationTitle, notificationMessage, null);
        notificationManager.notify();

        return null;
    }

after running the app i get this error :

09-27 12:55:57.836: E/AndroidRuntime(18715): FATAL EXCEPTION: main
09-27 12:55:57.836: E/AndroidRuntime(18715): Process: com.example.notify, PID: 18715
09-27 12:55:57.836: E/AndroidRuntime(18715): java.lang.IllegalMonitorStateException: object not locked by thread before notify()
09-27 12:55:57.836: E/AndroidRuntime(18715):    at java.lang.Object.notify(Native Method)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at com.example.notify.MainActivity$1.Notify(MainActivity.java:61)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at com.example.notify.MainActivity$1.onClick(MainActivity.java:35)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at android.view.View.performClick(View.java:5184)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at android.view.View$PerformClick.run(View.java:20910)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at android.os.Handler.handleCallback(Handler.java:739)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at android.os.Looper.loop(Looper.java:145)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at android.app.ActivityThread.main(ActivityThread.java:5942)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at java.lang.reflect.Method.invoke(Native Method)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at java.lang.reflect.Method.invoke(Method.java:372)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
09-27 12:55:57.836: E/AndroidRuntime(18715):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

My final goal is to set notification for tomorrow, next week and next month, and if you know how to use a method that is not deprecated I'll be very thankful to know, I also didn't have any receiver or implementation in manifest if there is needed such tell me.


Solution

  • Below is the activity with button click event. It will set the alarm with particular time. It will inform the broadcast receiver.

    Button notificationButton = (Button) findViewById(R.id.button1);
    
    notificationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(MainActivity.this , WakeUpReceiver.class);  
            myIntent.setAction("com.intent.action.MEETING_BUSINESS");
            myIntent.putExtra("Title","Meeting with Business");
            myIntent.putExtra("Message","Pittsburg 10:00 AM EST");
    
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(YourActivityName.this, 0,  myIntent , PendingIntent.FLAG_CANCEL_CURRENT);    
    
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            calendar.set(Calendar.MINUTE, 56);
            calendar.set(Calendar.SECOND, 00);
    
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
        }
    }
    

    The broadcast receiver is actually responsible for firing the notification.

    In your Android manifest.xml file add this intent filter and register the receiver:

    <receiver android:name="com.package.WakeUpReceiver">
       <intent-filter>
           <action android:name="com.intent.action.MEETING_BUSINESS" />
       </intent-filter>
    </receiver>
    

    Define the broadcast receiver class:

    packager com.package;
    
    public class WakeUpReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
        String action =  intent.getAction();
            if(action.equals("com.intent.action.MEETING_BUSINESS")){
                String title = intent.getStringExtra("Title");
                String message = intnet.getStringExtra("Message");
    
                // if you want to open the application when click on notification 
                // then pass the activity name here.
                PendingIntent intent1 = PendingIntent.getActivity(context,0, intent, 0); //<- pass your activity name here.
    
                Notification n  = new Notification.Builder(context)//<-- pass the context
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(intent1)
                .setAutoCancel(true).build();
    
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(0, n); 
            }
        }
    }