Is it possible to perform some function in an app activity when the notification is sent, without the user clicking on the Notification bar?
Example: I have an onClick method which is needed to be called when Notification is sent.
Yes from notificationReceiver onReceive() method you can directly call startActivity like below
Intent resultIntent = new Intent(getApplicationContext(), ActivityName.class);
resultIntent.setData(Uri.parse("content://"
+ Calendar.getInstance().getTimeInMillis()));
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(resultIntent);
Related
I have added two action button to the notification but I'm not able to handle the click event in background.
Intent ok = new Intent(mContext, MainActivity.class);
ok.putExtra("OK",true);
PendingIntent intentOK = PendingIntent.getActivity(mContext, 0 , ok, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_ok, delivered(), intentOK);
mBuilder.addAction(R.drawable.ic_ko, notdelivered(), intentKO);
The actions needs an intent that force my app in foreground.
I need to post user choice to a remote server without popping up the app.
I have an Intent that I initiate like this:
notificationIntent = new Intent(context, HomeActivity.class);
This intent is attached to an ongoing notification.
Now, in addition to the class opened when clicking the intent, I want to add an action string to the intent, so that when the notification clicked my custom BroadcastReceiver that listens to the same action string will trigger.
notificationIntent.setAction(context.getString(R.string.notification_clicked_action_string));
Problem is, for some reason, the BroadcastReceiver is not called, and I have other BroadcastReceiver that I registered problematically like this and they work fine.
So, it is a problem to have both a class and an action in an intent?
So, it is a problem to have both a class and an action in an intent?
No, that is perfectly fine.
However, unless you have a very strange naming system, HomeActivity is an activity. That means that new Intent(context, HomeActivity.class) identifies that activity, and you are hopefully using that with PendingIntent.getActivity(). You cannot have a BroadcastReceiver respond to startActivity(), which is what will be called when the PendingIntent is invoked.
I've followed this tutorial to create a new GCM Listener service:
http://www.appifiedtech.net/2015/08/01/android-gcm-push-notification-example/
The code for the listener service:
#Override
public void onMessageReceived(String from, Bundle data) {
super.onMessageReceived(from, data);
String msg = data.getString("message");
Log.d(TAG,"GCM Message received is "+msg);
// Notifying to user code goes here
notifyUser(getApplicationContext(),msg);
}
public void notifyUser(Context context,String data){
Intent intent = new Intent(context, NotificationActivity.class);
intent.putExtra("data", data);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
builder.setContentTitle("New Notification");
builder.setContentIntent(pendingIntent);
builder.setContentText(data);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
notificationManager.notify(countNotification++, builder.build());
Log.v(TAG,"count "+countNotification);
}
When the app is running (foreground), this works fine and launches the Notification Activity as it should.
However, when it's running in background, I get the notification but the title and body are as defined in my server sender application, and tapping on it takes me to the Main Activity.
This essentially means that when it's running in the background something else handles the notification? Is there another handler I should implement to be able to manage that notification and send the user to the correct activity?
The screen does not wake when I receive this notification, nor does an LED light up on the phone as notifications from other applications do. How do you manage that?
(permissions, services and receiver are defined in the manifest as described in the tutorial)
In problem regarding when your apps running in the background something else handles the notification?
This SO question can help you in answering your question regarding the GCM Listener Service
In the problem regarding to the screen that doesn't wake when you receive notification.
Use ACQUIRE_CAUSES_WAKEUP
Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
You can visit this SO question in how to use it.
You remove
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
Using intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
In the NotificationActivity class , you need implement the onNewIntent() callback when you open NotificationActivity existed the stack.
I'm a bit confused with pending intents in the notification builder. I've got a MainActivity activity and a MessageList activity. I have a service to show a notification when a new message in found, and I want it to be that if the user presses the notification it opens to the MessageList activity but when they press back they will return to the activity they were in.
Essentially I want to add MessageList activity to the top of the activity stack when they press the notification without modifying the current activity stack.
Thank you
Okay, so I got it to work with some old code I wrote a while back. This does what I wanted -
Intent notificationIntent = new Intent(this, AlertListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);
In my app I have a notification in the task bar and when clicked it starts a new Activity view of my program. However when I try and use the stop button to stop my service from within this view, nothing happens. I am assuming when I press the notification button that it does not keep my ACTIVITY linked with the service.
When my app is first opened, the service is started by using the Intent below:
Intent svc = new Intent(this, StreamService.class);
startService(svc);
Then in the service:
//Intent passed when notification is selected.
Intent notificationIntent = new Intent(this, HomeActivity.class);
Any idea how to link back to my running service? Or perhaps bring the original activity/view back to the front when the notification is selected?
The Intent that you can attach to the notification should have a service intent, with a specific action. In your service's onStart method, you can check it's a close action, and then you can call stopSelf().
If the Activity you are opening from the notification is part of the same application as the service, then it is be able to stop the service. Try to use:
Intent svc = new Intent(this, StreamService.class);
stopService(svc);