English 中文(简体)
如何建立不振动的iOS推力通知?
原标题:How can I create iOS push notifications that don’t vibrate?
I know how to create silent push notifications (with playing a sound file that is silent). I would also like to send a push notification that doesn t vibrate phone. When setting silent sound file as suggested below phone still vibrates when it is locked or when the app is not active. My payload that still vibrates: { aps = { alert = { "loc-key" = "SOME_KEY"; }; badge = 1; sound = "silence.caf"; }; } Is this possible?
问题回答
Omitting the sound key should do the trick: {"aps":{"alert":{"loc-key":"SOME_KEY"}, "badge":1} The docs state that "If the sound file doesn’t exist or default is specified as the value, the default alert sound is played.". What they don t say is that if you don t provide a sound key at all, no sound will be played. If no sound is played, the phone should not vibrate as well.
I did it this way: server send a push to device with sound file name which is silent. That s it. Example: Incomming payload from server: { aps = { "content-available" = 1; sound="silence.mp3" }; data-id = 1234; } And device handle it in AppDelegate: - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSInteger data_id= [userInfo[@"data-id"] integerValue]; NSLog(@"Data-id: %i", data_id); // // code... // // UIBackgroundFetchResultNoData/UIBackgroundFetchResultFailed/UIBackgroundFetchResultNewData completionHandler(UIBackgroundFetchResultNewData); }
when you register for push notification don t ask for sound type (UIRemoteNotificationTypeSound) - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types you can test this by manually removing the sound permission in the notification settings
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge]; Please try to register for remote notifications using above code applicationDidFinsihLaunching method of AppDelegate
I ve faced with similiar issue. Vibration can be fixed by setting background fetch in project capabilities
I have to add in AppDelegate : func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { NotificationCenter.default.post(name: Notification.Name.init("didReceiveRemoteNotification"), object: completionHandler, userInfo: userInfo) } And check "Remote notifications" in the Capabilities "Background Modes". Then I send a notification with only the data : device.send_message( messaging.Message( data = data, ), app = FCMApp)




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签