I have a music service playing in background. Whenever some action happens on notification i want to listen(Pending event) those actions in service for further processing. Please provide code samples if possible.
You can start service on notification click by two ways :
1) First Way : You can call broadcast receiver when user clicks on notification.
For this you need to use PendingIntent.getBroadcast instead of
getActivity().
Reference : How to Start one Activity and two service on notification click
2) Second Way : You can directly start service when user clicks on notification.
For this you need to use PendingIntent.getService instead of
getActivity().
Reference : Start Service from Notification
Related
It seems like both involve showing a notification and then the user can touch the notification to launch your activity.
https://developer.android.com/training/notify-user/build-notification
Every notification should respond to a tap, usually to open an
activity in your app that corresponds to the notification. To do so,
you must specify a content intent defined with a PendingIntent object
and pass it to setContentIntent().
https://developer.android.com/training/notify-user/time-sensitive
Use a full-screen intent only for the highest-priority alerts
where you have an associated activity that you would like to launch after the user
interacts with the notification.
So it seems like in both situations a notification shows that the user must interact with and then your activity is launched.
I'm implementing Parse push notification. I have 2 receivers. First one will always show notification and second one will take over notification if one particular activity is running. One registered in manifest (priority 1) and the other is registered/unregistered dynamically (priority 2). My problem is that I cant cancel the broadcast from my dynamic receiver by calling
abortBroadcast()
it throws an exception
BroadcastReceiver trying to return result during a non-ordered broadcast
So, is there a way to make the broadcast ordered or something like that?
I really want to handle the data in my activity when it is active.
Ok, after 2 hrs of searching, finally solved it. This is what I did,
First I created a base BroadcastReceiver which receives the push notification from Parse. It then removes all actions from received intent and adds a custom action eg, com.myclass.PUSH
Then I used
context.sendOrderedBroadcast(intent, null);
to send a new ordered broadcast using my custom action.
Now I set other two receiver's (the one defined in manifest and my dynamic receiver defined in activity) action to com.myclass.PUSH
Now the broadcast is ordered and i can cancel it using abortBroadcast()
I am implementing a music player. The notifications allow the user to pause or skip a song.
I use
Intent i = new Intent("com.package.app");
mExpandedView.setOnClickPendingIntent(R.id.next_song, PendingIntent.getBroadcast(this, 0, i, 0));
In order to transmit this click to the MusicService that hosts the MediaPlayer and all the associated methods. I would like to directly call a method part of this service (playNextSong() for example) but getService() seems to only allow me to launch a new service, not to call a method in the service, or get some data. I don't even need to launch the service, since the music is playing, it is already running.
So is there a way to do this that I am not aware of ?, or is :
Notification broadcasts to BroadcastReceiver, then BroadcastReceiver broadcasts to the service the recommended way do accomplish this action ?
It looks like a convoluted way to do something simple...
Create PendingIntent for notification as broadcast message, custom one (use your own string like com.my.custom.broadcast.message.action). Create and register in AndroidManifest new broadcast receiver that will be fired by this custom action. OnReceive method of the Broadcast receiver, start your service with custom arguments/action or whatever, based on class of Service and context arguments passed into onReceive method.
Probably you can try to directly start service by creating PendingIntent for that, but I think it is better do it through middle-step: BroadcastReceiver
From the Notification you can start an Activity. That activity would do "bindService" and call the appropriate method in the service, then finish(). The activity doesn't need to have a UI, so the user won't see it. But that's even more code than a Broadcastreceiver.
I would like to animate the icon of the app without having to cancel the notification and create a new one (because in this way the icon doesn't stay in the same position of the notification bar but could move to first place if there are other notification running).
I'm able to get this with normal notification, but I would like to get the same behaviour when I use startForeground in my service. This method launches a new notification which can't be removed unless you remove service from foreground using stopForeground.
Is this possible to do? How?
Use the same notification ID for the startForeground method and the Notification object.
I want to set a onClickListner for a status bar Notification. How it is possible ? Please help. Now i can load a Activity by using the pending intent. I like to set a onClickListner for the notificatioin.
Regards
Parvathi
It is not possible to set an OnClickListener for a notification. Because of the way notifications are handled/displayed there is no way to guarantee that the process that created the notification will be running at the time the notification is clicked. This means that any code you wrote to provide click handling may not be running.
If you need click listener style behavior you will have to do it using the PendingIntent: set it to start a Service that runs the logic or to use an Intent that is received by a BroadcastReceiver. This will let you perform activity without requiring a UI.