Can you mute incoming Push Notifications from the App side [iOS]

孤街醉人 提交于 2019-12-22 12:09:06

问题


I am making an app that uses Apples Push Notifications. I want to be able to have an "off duty" mode, in which the push notification is still received, but no sound is played when it comes in. Is there a way to mute these push notifications from inside the app? I know I can simply not send the sound from the JSON message, but it would be easier if I could do it from inside the app. I want to still receive the notifications, so I don't want to unregister from push notifications


回答1:


If your app is in background, the only way to mute the push is not send the sound from the JSON message.

When your app is foreground, you will receive push by application:didReceiveRemoteNotification:, in this case, whether your send sound or not, app will receive push silently.




回答2:


You can change the registration to not include sound.

When you want to sound the notifications, call:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

When you want to mute the notifications:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];

This will allow only the alert and badge to be displayed. The sound won't be played.

EDIT:

I believe the following quote implies that the notification won't be filtered if the payload contains a type that is not enabled. The device simply won't display/sound the type that is not enabled.

The system does not badge icons, display alert messages, or play alert sounds if any of these notifications types are not enabled for your app, even if they are specified in the notification payload.

You should also note that whatever types of notifications your app chooses to enable/disable, the users can override that decision manually:

Users can modify the enabled notification types at any point, using Settings in iOS or System Preferences in OS X.



来源:https://stackoverflow.com/questions/20004500/can-you-mute-incoming-push-notifications-from-the-app-side-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!