Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Push Notification are ON?

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

like image 520
goodm Avatar asked Jun 04 '13 08:06

goodm


2 Answers

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.

like image 120
Eran Avatar answered Oct 04 '22 22:10

Eran


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);
}
like image 44
user2719882 Avatar answered Oct 04 '22 21:10

user2719882