Pan Mluvčí
Pan Mluvčí

Reputation: 1261

Swift - GCM: Don't display iOS remote push notification

I have implemented GCM push notifications in my iOS app. I need the notifications only for syncing with new content. That works well. But I dont want the notification to be shown to the user. How do I hide it? enter image description here

Upvotes: 2

Views: 1444

Answers (2)

myra
myra

Reputation: 41

Just for not showing the notification the notification needs to be silent. To do this you can not have any title, body or subtitle in the message.

By default, iOS will ignore a message without title, body or subtitle so the extra parameter content_available: true is necessary.

So, if you send this message without any title, body and subtitle but with content_available: true still nothing would happen.

To respond to this, the app needs permission to run a small task in the background after receiving the scilent notification.

To get this permission you need to edit your .plist file and add the field Required background modes with the value remote-notifications. Or you can select the project and go to the Capabilities tab -> Background Modes -> Remote notifications. This will add the .plist entry for you.

Now you can do a small task in the background when receiving the notification. Like parse some custom data that was in the notification payload or do some light network calls etc.

you would want to look in your AppDelegate for:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

Here you can parse the userInfo data.

Upvotes: 0

ncremins
ncremins

Reputation: 9200

You need to send the content_available parameter to true

https://developers.google.com/cloud-messaging/http-server-ref

You also need to add remote-notifications for UIBackgroundModein your apps Info.plist file. https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22

Upvotes: 2

Related Questions