GCM 通知处理和避免 activity 打开如果在相同 activity
GCM Notification Handling and avoiding activity open if in same activity
我正在开发使用 GCM 服务的应用程序。
我可以使用代码
发送通知
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
主 activity 中的接收器就像
@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
}
当用户 1 通过 gcm 通知向另一个用户发送数据时,用户 2 收到了数据,并且 select 能够写入他设备上的文件
我遇到了一些问题,例如:
1. 当 User1 连续发送 3-4 条消息时,由于 mNotificationManager.notify(notifNo++, mBuilder.build());
所有消息都单独显示,我想避免并按顺序发送所有 3-4 条消息的列表作为通知。
2.当user2通过notification收到消息并且在同一个activity,避免notification直接写入文件/在activity接收。
注意:我这样做是为了多播推送通知,因此当任何用户离线时,折叠键可能不允许超过 4 users/messages。
如果重复并 post 建议链接,请原谅。
您可以使用以下方法创建一个助手 class 来跟踪当前打开的 activity。
/** 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);
}
我在 Whosebug 问题的帮助下找到了解决方案。
在 HandleIntent 上,我做了一些更改,例如
- 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."
为此,我删除了 notifNo++
以在同一通知中获取所有消息,我额外添加的是,
- 在更改
inActivity
状态时,我正在制作字符串 NotifData = ""
- 第一次通知时,我发送用户通知
NOTIFICATION_ID
- 当用户收到另一个通知时,附加
NotifData
换行符。 (您可以评论更好的方法)。
- 再次
onNewIntent()
,我正在制作 NotifData = ""
。
- When user2 receives message through notification and is in same activity, avoid notification and write to file/ receive in activity directly.
为此,我添加了一个静态布尔变量 inActivity
,只有当用户当前在 activity 屏幕上时才为真,即 onCreate(), onNewIntent(), onStart(), onPause()
,而在 onCreate(), onNewIntent(), onStart(), onPause()
等相应方法上为假 onStop()
等。基于此变量检查,我决定调用 mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
请检查 this 了解更多详细信息。
我正在开发使用 GCM 服务的应用程序。 我可以使用代码
发送通知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
主 activity 中的接收器就像
@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
}
当用户 1 通过 gcm 通知向另一个用户发送数据时,用户 2 收到了数据,并且 select 能够写入他设备上的文件
我遇到了一些问题,例如:
1. 当 User1 连续发送 3-4 条消息时,由于 mNotificationManager.notify(notifNo++, mBuilder.build());
所有消息都单独显示,我想避免并按顺序发送所有 3-4 条消息的列表作为通知。
2.当user2通过notification收到消息并且在同一个activity,避免notification直接写入文件/在activity接收。
注意:我这样做是为了多播推送通知,因此当任何用户离线时,折叠键可能不允许超过 4 users/messages。
如果重复并 post 建议链接,请原谅。
您可以使用以下方法创建一个助手 class 来跟踪当前打开的 activity。
/** 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);
}
我在 Whosebug 问题的帮助下找到了解决方案。 在 HandleIntent 上,我做了一些更改,例如
- 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."
为此,我删除了 notifNo++
以在同一通知中获取所有消息,我额外添加的是,
- 在更改
inActivity
状态时,我正在制作字符串NotifData = ""
- 第一次通知时,我发送用户通知
NOTIFICATION_ID
- 当用户收到另一个通知时,附加
NotifData
换行符。 (您可以评论更好的方法)。 - 再次
onNewIntent()
,我正在制作NotifData = ""
。
- When user2 receives message through notification and is in same activity, avoid notification and write to file/ receive in activity directly.
为此,我添加了一个静态布尔变量 inActivity
,只有当用户当前在 activity 屏幕上时才为真,即 onCreate(), onNewIntent(), onStart(), onPause()
,而在 onCreate(), onNewIntent(), onStart(), onPause()
等相应方法上为假 onStop()
等。基于此变量检查,我决定调用 mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
请检查 this 了解更多详细信息。