Handle multiple Notifications / Stacking Notifications from GCM

后端 未结 1 450
醉酒成梦
醉酒成梦 2021-02-06 02:22

I just implemented GCM and notifications in my Android app, coming from an Apache/PHP-based webserver.
The notifications are already working, but I\'m stuck at stacking the

相关标签:
1条回答
  • 2021-02-06 03:10

    I finally found the solution and ended up using atomic integers, but in a seperated class:

    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Global {
        public static AtomicInteger Counter1 = new AtomicInteger();
        public static AtomicInteger Counter2 = new AtomicInteger();
    }
    

    To reset the counter after the application opening, i put this in my MainActivity (called in onCreate() and onResume():

    private void clearNotifications(){        
        NotificationManager mNotificationManager;
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancelAll();
    
        Global.Counter1.set(0);
        Global.Counter2.set(0);
    }
    

    When I create the notification, I check the counter:

    Counter1 = Global.Counter1.incrementAndGet();
    ContentText = (Counter1 < 2) ? /* Single notification */ : /* Stacking */;
    
    0 讨论(0)
提交回复
热议问题