Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification Action not calling pending intent to service

I am trying to get my on going notification to have a button, when clicked it should call my service which is controlling audio playback.

Here is my notification with the intent

        Intent intent = new Intent(context, AudioStreamService.class);


    Random generator = new Random();


    PendingIntent i = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);


    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(title)
            .setContentText(text)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setLargeIcon(picture)
            .setTicker(ticker)
            .setNumber(number)
            .setOngoing(true)
            .addAction(
               R.drawable.ic_action_stat_reply,
               res.getString(R.string.action_reply),
               i);

    notify(context, builder.build());

Here is the on start for my service

    public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("APP ID", "APP ID - service called ");

    if(isPlaying){
        stop();
    }
    else {
        play();
    }
    return Service.START_STICKY;
}

The log is never triggered when called from the action in the notification. But the service is used by a button in the app and works fine. Log gets called as do commands below.

like image 755
Dandrews Avatar asked Jul 06 '14 04:07

Dandrews


People also ask

What is pending intent in Android notification?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

Which intent is employed for generating notification in Android?

MainActivity. The NotificationManager. notify() method is used to display the notification. The Intent class is used to call another activity (NotificationView. java) on taping the notification.

How do I make my Android notifications clickable?

1 Answer. Show activity on this post. setContentIntent(PROVIDE_YOUR_PENDING_INTENT); You will need to provide a pending intent to where you want to redirect the user when he click on the notification.


1 Answers

Use :

PendingIntent pendingIntent = PendingIntent.getService(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);

instead of :

PendingIntent pendingIntent = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);

^^ This is used to start an activity from the notification.

like image 153
Shivam Verma Avatar answered Oct 23 '22 13:10

Shivam Verma