0

I want to stop media player on clearing of notifications and also how to open some activity when i click on notification

this is my alarm function

    public void alarmstart(String idd,int alarmmonth,int alarmyear,int alarmday,int alarmhour,int alarmmin)
        {
            AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


 Intent myIntent;
        PendingIntent pendingIntent;

        myIntent = new Intent(Create.this,AlarmNotificationReceiver.class);
        myIntent.putExtra("title",tii);
        myIntent.putExtra("note",noo);
        myIntent.putExtra("id",id);
        pendingIntent = 
        PendingIntent.getBroadcast(this,Integer.valueOf(idd),myIntent,0);


        Calendar cal1=Calendar.getInstance();
        cal1.set(Calendar.MONTH,alarmmonth-1);
        cal1.set(Calendar.YEAR,alarmyear);
        cal1.set(Calendar.DAY_OF_MONTH,alarmday);

        cal1.set(Calendar.HOUR_OF_DAY,alarmhour);
        cal1.set(Calendar.MINUTE,alarmmin);
        cal1.set(Calendar.SECOND,0);

        manager.set(AlarmManager.RTC_WAKEUP,cal1.getTimeInMillis() ,pendingIntent);


    }

this is my notification class broadcast receiver

public class AlarmNotificationReceiver extends BroadcastReceiver {

    static MediaPlayer mMediaPlayer;
    PendingIntent pintent;
    static String id;



    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        String ti,no;
        ti=intent.getExtras().getString("title");
        no=intent.getExtras().getString("note");
        id=intent.getExtras().getString("id");

        Intent stopSoundIntent = new Intent(context,
                NotificationActionService.class)
                .setAction("Stop");


        PendingIntent stopSoundPendingIntent = PendingIntent.getService(context, 0,
                stopSoundIntent, PendingIntent.FLAG_ONE_SHOT);


       // builder.setAutoCancel(true)
                builder.setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(ti)
                .setContentText(no)

                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                .addAction(new NotificationCompat.Action(R.mipmap.stop,
                        "Stop", stopSoundPendingIntent))


                .setContentInfo("Info");
         mMediaPlayer = MediaPlayer.create(context, R.raw.teri);
         mMediaPlayer.start();

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Integer.valueOf(id),builder.build());


    }

    public static class NotificationActionService extends IntentService {
        public NotificationActionService() {
            super(NotificationActionService.class.getSimpleName());
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            String action = intent.getAction();

            if ("Stop".equals(action)) {
                // TODO: handle action StopSound.
                mMediaPlayer.stop();
                // If you want to cancel the notification:
                NotificationManagerCompat.from(this).cancel(Integer.valueOf(id));
                // NOTIFICATION_ID : you can (set and get) notificationid (to/from) intent
            }

        }

    }
}

please can anyone help me on this I am newbie on this almost read the all previous posts related to this topic but not able to do this I am not able to use deleteintent correctly and also how to open activity upon clicking on notification

1 Answers1

0

To open activity on clicking on notification,

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, HomeActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
        notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

To clear notification onClick,

notification.flags |= Notification.FLAG_AUTO_CANCEL;

After opening of MainActivity you can perform your stuff.

Dhruv Patel
  • 1,529
  • 1
  • 18
  • 27
  • i dont want to clear notification onclick i want to stop mediaplayer whenever notification is cleared – Jibran Shabir Nov 13 '17 at 10:58
  • Use BroadCastReceiver to generate an action. https://stackoverflow.com/questions/15350998/determine-addaction-click-for-android-notifications – Dhruv Patel Nov 13 '17 at 11:06
  • you are not understanding what i want to to do :( there is a delete intent search it. I want to implement delete intent but not able to do it – Jibran Shabir Nov 13 '17 at 11:37
  • This does not answer what the user asked. He asked how to perform an action when the user clicks on the "Clear all" option available in the notification shade while this answer explains how to perform an action when the user clicks on a notification, completely unrelated. – Shadow Oct 08 '19 at 08:38