0

I am working on application which uses GCM service. I'm able to send notifications with code

mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent msgMainActivity = new Intent(this, MainActivity.class);
msgMainActivity.putExtra("msg", msg);
msgMainActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,msgMainActivity, PendingIntent.FLAG_CANCEL_CURRENT);

//  contentIntent.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("appName Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(sndr+": "+msg)
            .setNumber(notifNo); //static id notifNo

// Set notification sound
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mBuilder.setSound(alarmSound);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(notifNo++, mBuilder.build()); // separate notifications each time

And receiver in main activity is like

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle notificationData = intent.getExtras();
String notifMessage = notificationData.getString("msg");
// rest code for appending notifMessage in file.txt
}

When user1 sends data to another user via gcm notification, user2 received it and on select able to write to file at his device I'm facing some problems like:
1. When User1 sends 3-4 messages continuously, all are showed separately because of mNotificationManager.notify(notifNo++, mBuilder.build()); , which i want to avoid and send list of all 3-4 msgs in sequence as a notification.
2. When user2 receives message through notification and is in same activity, avoid notification and write to file/ receive in activity directly.
Note: I'm doing this for multicast push notification, hence collapse key might not allow more than 4 users/messages when any of the users is offline.
Kindly pardon if repeating and post suggesting links.

yrazlik
  • 10,411
  • 33
  • 99
  • 165
kAmol
  • 1,297
  • 1
  • 18
  • 29

2 Answers2

0

You can create a helper class with the following methods that tracks which activity is currently open.

/** A set which maintains the visible activities **/
private static final Collection<String> sVisiblePages = new HashSet<>();

/** Call this when your Page becomes visible, tracking ID is the 
unique ID for an activity (You can use class name) */
public static void markPageAsVisible(String trackingID){
    sVisiblePages.add(trackingID);
}

 /** Whether or not a page with this tracking ID is visible to the user. */
public static boolean isPageVisible(String trackingID){
    return sVisiblePages.contains(trackingID);
}

/**
 * Marks this page as hidden upon validating that it is indeed going into background
 * and not getting re-created due to an orientation change.
 * */
public static void markPageAsHidden(Activity activity, String trackingID){
    if(!activity.isChangingConfigurations()) markPageAsHidden(trackingID);
}
Abhay Sood
  • 480
  • 2
  • 6
  • 20
  • Thanks for the reply, but I couldn't get how is it helping and to which point you are answering.. – kAmol Sep 13 '15 at 09:45
0

I figured out solution with some help over StackOverflow questions. On HandleIntent, I did some changes like,

  1. When User1 sends 3-4 messages continuously, all are showed separately because of mNotificationManager.notify(notifNo++, mBuilder.build()); , which i want to avoid and send list of all 3-4 msgs in sequence as a notification."

For this, I've removed notifNo++ to get all messages in same notification, what I've added extra is,

  • while changing inActivity status, I'm making String NotifData = ""
  • On first notification, I'm sending user notification with NOTIFICATION_ID
  • When user gets another notification, append NotifData with new line character. (you may comment better approach).
  • Again on onNewIntent(), I'm making NotifData = "".
  1. When user2 receives message through notification and is in same activity, avoid notification and write to file/ receive in activity directly.

For this I've added one static boolean variable inActivity, which will be true only if user is currently on activity screen i.e. onCreate(), onNewIntent(), onStart(), onPause() and false on respective methods like onStop() etc. Based on this variable check, I'm deciding to call mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); Please check this for more details.

Community
  • 1
  • 1
kAmol
  • 1,297
  • 1
  • 18
  • 29