2

I have a big problem, I set up (with great difficulty) push notifications on my iOS project. I decided to receive the data of the notification in the "didReceiveRemoteNotification" method of the "AppDelegate" then to create it programmatically (in order to carry out mandatory personal treatment). Everything works perfectly, only when I launch my application without the help of Xcode, I no longer receive the notifications, the notification creation code is not executed. I do not know what to do ...

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

    let body = userInfo["body"]

    print(userInfo)

    if #available(iOS 10.0, *) {
        let content = UNMutableNotificationContent()
        content.body = body! as! String
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 
1, repeats: false)

        let request = UNNotificationRequest(identifier: "idt", content: 
content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, 
withCompletionHandler: nil)

    } else {
        // Fallback on earlier versions
    }

    completionHandler(UIBackgroundFetchResult.newData)
}

Thanks you very much

Umberto Emonds
  • 121
  • 1
  • 5

5 Answers5

1

When you launch your app from a notification you need to check the launchOptions in application:didFinishLaunchingWithOptions: to see if UIApplicationLaunchOptionsRemoteNotificationKey is present.

According to the Documentation:

The value of this key is an NSDictionary containing the payload of the remote notification. See the description of application:didReceiveRemoteNotification: for further information about handling remote notifications. This key is also used to access the same value in the userInfo dictionary of the notification named UIApplicationDidFinishLaunchingNotification.

Once you determine that the launch contains a notification, invoke your notification handler method with the notification payload obtained from launchOptions.

What you have now, which is application(_:didReceiveRemoteNotification:) is only triggered when the app is already running:

If the app is running, the app calls this method to process incoming remote notifications. The userInfo dictionary contains the aps key whose value is another dictionary with the remaining notification

jvrmed
  • 786
  • 5
  • 12
0

You need to check in your project settings/Capabilities - Background Modes - Remote notifications

Radovan78
  • 1
  • 1
0

Enable Remote notifications in Capabilities.

shiju86.v
  • 647
  • 5
  • 10
0
 {
   "aps" : {
      "content-available" : 1,
        "badge" : 0,
        "Priority" : 10
   },
   "acme1" : "bar",
   "acme2" : 42
}

The notification’s priority:

10 The push message is sent immediately.

The remote notification must trigger an alert, sound, or badge on the device. It is an error to use this priority for a push that contains only the content-available key.

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/BinaryProviderAPI.html#//apple_ref/doc/uid/TP40008194-CH13-SW1

THess
  • 1,001
  • 1
  • 9
  • 19
-1

When the device is running and connected to Xcode, the system treats silent notifications as high-priority simply because the OS is smart enough to know "You must be debugging your app, so I will treat your app's tasks as high priority".

When the device is disconnected from Xcode, the system then treats silent notifications as low-priority. Therefore, it doesn't guarantee their delivery in order to improve battery life.