My question is a little complicated, so I will briefly describe what I want to achieve. My application receives messages from GCMwithout any problems. When the application is running and visible to the user, I do not show a notification in the action bar, I show this message in a dialog message. I use BroadcastReceiverto achieve this.
The main problem is that when the application receives several notifications in the shortest possible time. When the main action is displayed to the user, the first notification will be displayed in a dialog warning. And the next one will be posted in the Action Bar, as usual, for Android notifications. And at that moment, the user opens a dialog warning and is about to select the next notification from the action bar. At this point, I use the flag in my CloudService(extends IntentService) , as well as for . - it's just for mine .intentIntent.FLAG_ACTIVITY_CLEAR_TOPPendingIntent.FLAG_UPDATE_CURRENTpendingIntentpendingIntentcontentIntentNotificationCompat.Builder
It works in such a way that each user clicks on a notification from the action bar, my activity is updated (floats to the bottom edge of the device and then floats from the top edge with a warning dialog with a message - I get additional data from the target in the method onResume). This action is quite normal. In this case, the activity has only one instance - I do not need to break through several instances of the same action until I opened several notifications. But the big problem is that when the user selects any of the notifications, in each case the latter will open in a dialog warning. It seems that the activity has the same intent, despite PendingIntent.FLAG_UPDATE_CURRENT.
There CloudServiceare two methods that process cloud messages:
private void showNotification(Bundle extras) {
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String message = extras.getString(CloudMetaData.MESSAGE);
if (App.isMyActivityVisible() && !CloudMessageDialogFragment.isAttached()) {
sendBroadcast(message, NOTIFICATION_ID);
} else {
Intent intent = new Intent(this, MyParentActivity.class);
intent.putExtra(CloudMetaData.MESSAGE, message);
intent.putExtra(CloudMetaData.NOTIFICATION_ID, NOTIFICATION_ID);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(getString(R.string.app_name));
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(alert));
builder.setContentText(alert);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setContentIntent(contentIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
NOTIFICATION_ID++;
}
, , :
private void sendBroadcast(String message, int notificationId) {
Intent intent = new Intent();
intent.setAction(ACTION_FROM_CLOUD_SERVICE);
intent.putExtra(CloudMetaData.MESSAGE, message);
intent.putExtra(CloudMetaData.NOTIFICATION_ID, notificationId);
sendBroadcast(intent);
}
? , , .
. AndroidManifest:
<activity
android:name="com.myapp.activity.CloudMessageDialogActivity"
android:theme="@android:style/Theme.Dialog"
android:parentActivityName="com.myapp.activity.MyParentActivity"/>
- , .