简体   繁体   中英

Azure disable push notifications android

I am switching my android application's push notification system from Urban Airship to Azure. Receiving notifications is working fine, but disabling the notifications is not working. I have a button in my settings menu which can toggle whether or not the user receives push notifications. Currently the method the button calls looks like this:

public static void setPushEnabled( Context context, boolean isEnabled) {
    if( isEnabled ) {
        NotificationsManager.handleNotifications(context, SENDER_ID, MyPushHandler.class);
    } else {
        NotificationsManager.stopHandlingNotifications(context);
    }
}

The app still receives push notifications after stopHandlingNotifications is called. Then if a user toggles push notifications back on, handleNotifications is called again, and the user receives 2 notifications for every one sent from the server. Here's the documentation for the NotificationsManager: http://dl.windowsazure.com/androiddocs/com/microsoft/windowsazure/notifications/NotificationsManager.html

Am I missing something here? Does anyone know any other ways to disable Azure push notifications?

What I was missing was the onUnregistered handler in the MyPushHandler class, this code fixed it:

@Override
public void onUnregistered(Context context, final String gcmRegistrationId) {
    super.onUnregistered(context, gcmRegistrationId);

    new AsyncTask<Void, Void, Void>() {
        protected Void doInBackground(Void... params) {
            try {
                mClient.getPush().unregisterAll(gcmRegistrationId);
                return null;
            }
            catch(Exception e) {

            }
            return null;
        }
    }.execute();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM