Reputation: 961
I have an app who is receiving a GCM notification and link to this notification a pendingIntent to open an URL:
private void sendNotification(String message) {
PendingIntent pendingIntent;
Intent intent;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (url != null) {
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
it works fine but I would like to be informed when this intent is opened (end user click on the notification to open the URL).
I tried to use the OnFinished callback but by doing this:
PendingIntent.OnFinished finish = new PendingIntent.OnFinished() {
public void onSendFinished(PendingIntent pi, Intent intent,
int resultCode, String resultData, Bundle resultExtras) {
//sending information to Ordolink server as user opened the URL
...do my stuff....
}
};
try {
pendingIntent.send(Activity.RESULT_OK, finish, null);
} catch (CanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pendingIntent is opened as soon as the notification is received
I tried by using BroadcastReceiver like this:
my activity:
Intent intent_broadcast = new Intent(this, MyBroadcastReceiver.class);
intent_broadcast.putExtra("url", url);
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent_broadcast, PendingIntent.FLAG_ONE_SHOT);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent_broadcast,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
the BroadcastReceiver :
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("JRE", "Callback onReceive Intent open URL");
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setData(Uri.parse(intent.getStringExtra("url")));
context.startActivity(myIntent);
}
}
and I also declared the BroadcastReceiver in the Manifest:
<receiver android:name="MyBroadcastReceiver" >
</receiver>
but the MyBroadcastReceiver is never called. any idea ?
Upvotes: 1
Views: 4509
Reputation: 7523
I'd suggest to use a BroadcastReceiver
in your app as the Intent
for PendingIntent
that would open the URL and in that BroadcastReceiver
you can know when the user opened the notification.
Example code -
PendingIntent pendingIntent;
Intent intent;
NotificationManager notificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
if (url != null) {
intent = new Intent(context, MyBroadcastReceiver.class);
intent.putExtra("url", url);
pendingIntent = PendingIntent.getBroadcast(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
}
And in your BroadcastReceiver
-
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
/* Whatever you want to do */
...
/* Finally open the URL */
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setData(Uri.parse(intent.getStrinExtra("url")));
context.startActivity(myIntent);
}
}
Upvotes: 2