Android - Push Notification are ON?

21,251

Solution 1

Assuming you are referring to Google Cloud Messaging (since you are using the android and push-notification tag), there are no general settings used to enable/disable the GCM service (unlike the Apple Push Notifications service for iOS devices).

When you install an app, if the app uses Google Cloud Messaging, it will be listed in the list of permissions that this app requires in order to work. By choosing to install the app you allow it to send push notifications to you.

That said, in order for the app to actually receive GCM messages, your app must programmatically register to the GCM service. You can do that in any place you wish within your app, and you can create an app settings activity (or fragment or whatever) in which the user can enable/disable GCM (which would trigger registration/unregistration to/from GCM). Your app can contain a persistent store that would hold the registration ID you receive upon registration to GCM, and you can use that store to determine if your app in registered to GCM.

You can ask your user to turn it on if the app is already running (if you have some code that does that). If it's not running and the app is not registered for GCM on that device, your server can't send GCM messages to it.

If you want to overcome that obstacle, you can register to GCM automatically (when the app is first launched). Then you can send messages to the app whenever you wish. If you still want to let the user decide whether they want to see those notifications, you can have a flag in your app settings that enables/disables the UI notification that you create as a result of an incoming GCM message. The GCM service will always be enabled, but unless the user chooses to view the notifications, you app won't display anything when a GCM message arrives.

Solution 2

you can't access this property unless you declare the system process or have root permission, first you can see the permission of file: /data/system/notifycaiton_policy.xml: -wr------。

In aother way, see the source code of: /android/4.2/frameworks/base/services/java/com/android/server/NotificationManagerService.java. in api: public void setNotificationsEnabledForPackage(String pkg, boolean enabled), there will check the caller's UID whether be Process.SYSTEM_UID:

void checkCallerIsSystem() {
    int uid = Binder.getCallingUid();
    if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
        return;
    }
    throw new SecurityException("Disallowed call for uid " + uid);
}
Share:
21,251

Related videos on Youtube

goodm
Author by

goodm

AS3/Android/Unity3D Lead interactive developer in digital production company based in London.

Updated on May 08, 2020

Comments

  • goodm
    goodm almost 4 years

    How to check programmatically if user turn off push notification in app settings? Can I open app settings intent directly from the app to prompt user to turn it on?

    enter image description here

    Thanks

  • goodm
    goodm almost 11 years
    So If I understand it right, "show notification" in app settings is just to hide all "popups" from the app? For example I can still receive notification, store them and show as a dialog when user enter the app.
  • Eran
    Eran almost 11 years
    @goodm I'm not sure which app settings you are referring to. Are you referring to specific app settings within a specific app? If you are referring to notifications that arrive from the GCM service, it's up to you do decide how to write your app (whether to automatically register to GCM and always stay registered, which will allow you to always receive notifications).
  • goodm
    goodm almost 11 years
    By app settings I mean Settings -> App Manager -> App info, there is small "tick" option called "Show notifications"
  • Eran
    Eran almost 11 years
    @goodm Which device/Android version are you using? I don't have it on my Android device (which is relatively old).
  • Eran
    Eran almost 11 years
    @goodm Sorry about that, I have S with 2.2, and I don't have that checkbox, so I wasn't aware of it. I thought you were referring to some settings in a specific app.

Related