Reputation: 8035
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?
Upvotes: 9
Views: 7329
Reputation: 372
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)
Upvotes: 0
Reputation: 159
I've faced with similiar issue. Vibration can be fixed by setting background fetch in project capabilities
Upvotes: 0
Reputation: 711
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge];
Please try to register for remote notifications using above code applicationDidFinsihLaunching method of AppDelegate
Upvotes: 0
Reputation: 10754
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.
Upvotes: 12
Reputation: 4980
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
Upvotes: 0
Reputation: 1535
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);
}
Upvotes: 0