【问题标题】:GCM Notification Handling and avoiding activity open if in same activity如果在同一活动中,GCM 通知处理和避免活动打开
【发布时间】:2015-09-13 07:56:22
【问题描述】:

我正在开发使用 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

主要活动中的接收者就像

@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 收到了它并且在选择时可以在他的设备上写入文件 我面临一些问题,例如:
1.当User1连续发送3-4条消息时,由于mNotificationManager.notify(notifNo++, mBuilder.build());,所有消息都单独显示,我想避免这种情况并按顺序发送所有3-4条消息的列表作为通知。
2.当user2通过notification收到消息并且在同一个activity中时,避免notification直接写入文件/在activity中接收。
注意:我这样做是为了多播推送通知,因此当任何用户离线时,折叠键可能不允许超过 4 个用户/消息。
如果重复并发布建议链接,请原谅。

【问题讨论】:

    标签: android notifications push-notification google-cloud-messaging


    【解决方案1】:

    您可以使用以下方法创建一个帮助类来跟踪当前打开的活动。

    /** 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);
    }
    

    【讨论】:

    • 感谢您的回复,但我不知道它对您有什么帮助以及您的回答是什么......
    【解决方案2】:

    我在 StackOverflow 问题的一些帮助下找到了解决方案。 在 HandleIntent 上,我做了一些更改,例如,

    1. 当User1连续发送3-4条消息时,由于mNotificationManager.notify(notifNo++, mBuilder.build());全部分开显示,我想避免这种情况,并按顺序发送所有 3-4 条消息的列表作为通知。”

    为此,我删除了 notifNo++ 以在同一通知中获取所有消息,我额外添加的是,

    • 在更改inActivity 状态时,我正在制作字符串NotifData = ""
    • 在第一次通知时,我使用NOTIFICATION_ID 发送用户通知
    • 当用户收到另一个通知时,在NotifData 后面加上换行符。 (您可以评论更好的方法)。
    • 又是onNewIntent(),我正在制作NotifData = ""
    1. 当用户2通过通知接收消息并且处于同一活动中时,避免通知并直接写入文件/在活动中接收。

    为此,我添加了一个静态布尔变量inActivity,仅当用户当前在活动屏幕上时才为真,即onCreate(), onNewIntent(), onStart(), onPause(),而在onStop()等相应方法上为假。基于此变量检查,我决定致电mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 了解更多详情,请查看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多