Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid gcm and PendingIntent
    primarykey
    data
    text
    <p>When I receive a push, I start an activity which decides what we do. To generate a notification I use the following code :</p> <pre><code>public class MyGcmBroadcastReceiver extends BroadcastReceiver { private Context ctx; private static final int NOTIFICATION_ID = 1; private NotificationCompat.Builder builder; private NotificationManager mNotificationManager; @Override public void onReceive(Context context, Intent intent) { GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); ctx = context; String messageType = gcm.getMessageType(intent); if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { // sendNotification("Send error: " + intent.getExtras().toString(), "", ""); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { // sendNotification("Deleted messages on server: " + intent.getExtras().toString(), "", ""); } else { String alert = intent.getExtras().getString("alert"); String key = intent.getExtras().getString("k"); String id = (String) intent.getExtras().get("i"); sendNotification(alert, key, Long.parseLong(id)); } setResultCode(Activity.RESULT_OK); } // Put the GCM message into a notification and post it. private void sendNotification(String msg, String key, long id) { mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(ctx, FakePushActivity_.class); notificationIntent.putExtra(Constants.EXTRA_ORDER_ID, id); notificationIntent.putExtra(Constants.EXTRA_KEY_PUSH, key); notificationIntent.putExtra(Constants.EXTRA_MSG_PUSH, msg); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0); // long[] vibraPattern = { 0, 500, 250, 500 }; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setContentTitle(ctx.getString(R.string.app_name)) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } } </code></pre> <p>My <code>FakePushActivity</code> is a simple activity which test some parameters to know what activity starts. For example, if the app is running so we display a <code>DialogFragment</code> else we run the app. Everything works when the app is running. I can see pushes and when I click on the push my activity is starting.</p> <p>If I wake up the app with the push, it is ok but the next push does not start the FakePushActivity anymore. I can see and click it but nothing happens on the click (the push only disappears). And the user has to start the app again to receive new pushes.</p> <p>It seems there is a problem with <code>PendingIntent</code> but I have no error in my log. </p> <p>Do you know what is the problem ?</p> <p>Thx in advance</p> <hr> <p><strong>[[ EDIT ]]</strong> : I've finally found a solution but I don't understand why it works. If somebody can explain me because I find it is not very clear this story about intent flag.</p> <pre><code>notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload