0

I can't make notification work when app is closed.

I am sending this from FCM/GCM(python server):

{
  "registration_ids": ["APA91bEDB9dVf-..."]
  "collapse_key": null,
  "content_available": true,
  "data": {
    "msg": "{\"model\":\"article\",\"data\":\"reload\"}"
  },
  "notification": {
    "body": "my body",
    "sound": "default",
    "title": "my title"
  },
  "priority": "high",
  "time_to_live": 3600
}

so curl is like this:

curl --header "Authorization: key=MYKEY" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d '{"registration_ids": ["APA91bEDB9dVf-..."],"collapse_key": null,"content_available": true,"notification": {"body": "my body","sound": "default","title": "my title"},"priority": "high","time_to_live": 3600,"data": {"msg":"{\"model\":\"article\",\"data\":\"reload\"}"}}'

and response in terminal is:

{"multicast_id":6089491211057070352,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1471474398816666%cc062fb9cc062fb9"}]}

in iOS app Background Modes is ON and Remote notifications:

enter image description here

AppDelegate.swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    FIRApp.configure()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)

    return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    let notification = UILocalNotification()
    notification.alertBody = "Test"
    notification.alertAction = "open"
    notification.fireDate = NSDate(timeIntervalSinceNow: 1)
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["UUID": 12312]

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
}

But still nothing shows when i send that msg, but when app is opened notification works.

What can be problem?

Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
  • Specifying the `data` key stopped notifications working in iOS for me (but seems to be required for Android notifications now). If it is working in app, it is probably being sent silently. Does it display a notification if you remove the `data` key? – Yasir Aug 17 '16 at 21:38
  • It doesn't, still same. – Mirza Delic Aug 17 '16 at 22:55
  • FYI not an answer: `didReceiveRemoteNotification` is only called when the PN arrives with your app in foreground or when the user taps the notification in notification center. I don't know why the PNs aren't showing up but it feels weird to have the local notification in `didReceiveRemoteNotification`, the user will see two notifications: one when the app is in background and one when after they tap it. – Felipe Cypriano Aug 17 '16 at 23:11
  • Not sure if this helps but did you check if your format as stated in [this thread](http://stackoverflow.com/questions/31109514/making-gcm-work-for-ios-device-in-the-background) may have been the problem? – ReyAnthonyRenacia Aug 22 '16 at 03:07
  • I did, tried, but still nothing. – Mirza Delic Aug 22 '16 at 07:29

1 Answers1

-2

{ "to":"ID", "notification":{
"sound":"default", "title":"TITLE", "body":"BODY" }, "priority": "high" } create notification in this pattern