latha
latha

Reputation: 41

how can i get notification for every 5 minutes up to user has to launch that notification from notification bar in android?

I am new to android, I am developing an application for every day notifications using alarm manager and receiver, and notifications too. But now i am facing two problems

  1. If the device is switched off , I am not getting any notification(I think it terminating permanently) and

  2. i need to show a notification every 5 minutes until the user launches the app using the notication from notification bar, but I am not sure how.

    public class AlarmReciever extends WakefulBroadcastReceiver {
    
      AlarmManager alarmManager;
      PendingIntent pendingIntent;
      int RequestCode =777;
    
      DBHelper helper;
      SQLiteDatabase database;
      SQLiteStatement statement;
      Cursor cursor;
      int datacount=0;
    
      public void onReceive(Context context, Intent intent) {
          String rec = intent.getDataString();
          NotifyMessage(context);
      }
    
      public void NotifyMessage(Context context){
          Intent intent = new Intent(context, NotificationTabs.class);
          intent.putExtra("Medicine", "Medicine");
          PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("Take medicine")
        .setContentTitle(context.getString(R.string.notificationtitle))
        .setContentText("Take medicine")
        .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
        .setContentIntent(pIntent)
        .setAutoCancel(true);
    
    
          NotificationManager notificationmanager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationmanager.notify(0, builder.build());
    
    }
    

Can I please get some help on how to solve these issues?

Upvotes: 1

Views: 1649

Answers (1)

Lips_coder
Lips_coder

Reputation: 686

I have done a small proof of concept for your second problem.Please add the following code in your activity:

Intent notificationIntent=new Intent(context,DisplayNotification.class);
        PendingIntent contentIntent=PendingIntent.getService(context,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.cancel(contentIntent);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),( AlarmManager.INTERVAL_FIFTEEN_MINUTES)/3, contentIntent);

Here is my DisplayNotification Service class:

public class DisplayNotification extends Service {
    private final static String TAG = "ShowNotification";

    @Override
    public void onCreate() {
        super.onCreate();
        Intent mainIntent=new Intent(this,MainActivity.class);
        NotificationManager notificationManager= (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification noti=new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentIntent(PendingIntent.getActivity(this,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT))
                .setContentTitle("Test Notification " + System.currentTimeMillis())
                .setContentText("Cricket news Dhoni is bold")
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.bell)
                .setTicker("match update")
                .setWhen(System.currentTimeMillis())
                .build();
        notificationManager.notify(0, noti);

        Log.i(TAG, "Notification created");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Receiver Class code:

public class MyBroadcastReceiver extends BroadcastReceiver {  
    MediaPlayer mp;  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        mp=MediaPlayer.create(context, R.raw.alrm   );  
        mp.start();  
        Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();  
    }  
}

Please declare the receiver in manifest:

  <uses-permission android:name="android.permission.VIBRATE" /> 


    <receiver android:name="MyBroadcastReceiver" >  
            </receiver>  

Upvotes: 0

Related Questions