1

Good day, I hope that someone will be able to help me! I have AlarmManager and NotificationCompat in my application. I want to stop playing alarm when I just click on notification. Here's my code:

public class MyAlarm extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 101;

@Override
public void onReceive(final Context context, Intent intent) {
    NotificationHelper notificationHelper = new NotificationHelper(context);
    MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
    Uri uri = intent.getData();

    Intent newIntent = new Intent(context, AddNoteActivity.class);
    newIntent.setData(uri);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder nb = notificationHelper.getChannel1Notification(pendingIntent);

    notificationHelper.getManager().notify(NOTIFICATION_ID, nb.build());

    mediaPlayer.start();
}

NotificationHelper:

public class NotificationHelper extends ContextWrapper {

public static final String chanel1ID = "chanel1ID";
public static final String chanel1Name = "chanel 1";

private NotificationManager notificationManager;

public NotificationHelper(Context base) {
    super(base);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        createChannels();
    }
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels(){
    NotificationChannel channel1 = new NotificationChannel(chanel1ID, chanel1Name, NotificationManager.IMPORTANCE_DEFAULT);
    channel1.enableLights(true);
    channel1.enableVibration(true);
    channel1.setLightColor(R.color.colorPrimary);
    channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(channel1);
}

public NotificationManager getManager() {
    if (notificationManager == null) {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    return notificationManager;
}

public NotificationCompat.Builder getChannel1Notification(PendingIntent intent){

    return new NotificationCompat.Builder(getApplicationContext(), chanel1ID)
            .setContentTitle("asda")
            .setContentIntent(intent)
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
            .setAutoCancel(true);
}

and setAlarm in 3rd class:

public void setAlarm(long timeInMillis){

    if(Build.VERSION.SDK_INT >= 23){
        mCalendar.set(
                mCalendar.get(Calendar.MONTH),
                mCalendar.get(Calendar.YEAR),
                mCalendar.get(Calendar.DAY_OF_YEAR),
                mCalendar.get(Calendar.HOUR_OF_DAY),
                mCalendar.get(Calendar.MINUTE)
        );
    }

    final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(this, MyAlarm.class);
    intent.setData(currentUri);

    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setExact(AlarmManager.RTC, timeInMillis, pendingIntent);

Hope someone will help me here, because I lost almost all day for find a solution. Thank you for your time!

newbieHere
  • 276
  • 2
  • 15
  • I thought will be possible to check in `public class MyAlarm extends BroadcastReceiver` is media player playing, if yes, after click on notification just stop playing... Now i know that I was wrong... – newbieHere May 16 '18 at 19:10

1 Answers1

0

NotificationCompat.Builder.setContentIntent

Supply a PendingIntent to send when the notification is clicked.

You're using

Intent newIntent = new Intent(context, AddNoteActivity.class);

So, in your AddNoteActivity you need to get the instance of your MediaPlayer and call stop().

  • Thanks so much for your answer! I will try to do it – newbieHere May 16 '18 at 20:46
  • Maybe you know, is it possible to check if user clicked on Notification? – newbieHere May 16 '18 at 20:53
  • I think it should simplify a lot of my job, because I just could check in onReceive if "notification was clicked -> stop play" ... Probably I'm wrong, correct me if it's true. (Sorry couldn't edit last comment) – newbieHere May 16 '18 at 21:01
  • @newbieHere AddNoteActivity is launched when the user clicks on the notification. This should help in distinguishing whether the activity is opened from the notification https://stackoverflow.com/questions/14122085/determine-if-activity-is-called-by-a-notification –  May 16 '18 at 21:03
  • @newbieHere no, it won't because onReceive is called by the AlarmManager when it triggers your alarm. –  May 16 '18 at 21:04
  • Ok thanks. I just thought, because in this method i built NotificationCompat. Thanks so much for your help! – newbieHere May 16 '18 at 21:09