Silent push notification (APNS) can not received - ios

I have set up the silent push notification for my app:
1. I configured the push notification from all places, i.e., XCode, Apple Developer portal with proper certificate
2. I enabled background capability
3. I included "content-available" in the json payload.
However, my App can not receive silent push sometimes.
To be more specific, neither
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
......
}
nor
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (_: UIBackgroundFetchResult) -> Void) {
......
}
has been invoked when the push message arrives.
How can I get the information in the push notification in such state?
I'm waiting online.

You question is twofold:
1. why the two delegate callbacks can not be invoked
2. what should you do to receive the data.
I did some hand test and here is the result:
1, when an app is in killed state, the two callback can not be invoked indeed
2. however, when you open the app next time,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
......
}
can be invoked and you can get the data from there.
I hope this is useful.

Related

How to configure Pusher Beams SDK with Firebase in App Delegate?

I have the Firebase SDK in my project for the backend and I am using the Beams SDK from Pusher to configure push notifications.
The issue is that, it doesn't seem that my app delegate is configuring the settings I need to send notification with the Beams SDK if I have Firebase. Here is what I mean.
According to the Beams SDK I need to place this code in the didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
self.pushNotifications.start(instanceId: "MyInstanceID")
self.pushNotifications.registerForRemoteNotifications()
try? self.pushNotifications.addDeviceInterest(interest: "debug-hello")
// FirebaseApp.configure() . However it ONLY works properly when this is commented out
return true
}
As I showed above, it only configures my notifications properly if I comment out the Firebase configuration. Here is other relevant code in my delegate:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("RECEIVED NOTIFICATION")
self.pushNotifications.handleNotification(userInfo: userInfo)
if Auth.auth().canHandleNotification(userInfo){
completionHandler(.noData)
return
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Did register for remote notifications")
self.pushNotifications.registerDeviceToken(deviceToken)
Auth.auth().setAPNSToken(deviceToken, type: .prod)
}
How do I properly configure both in my project?
The issue is that Firebase.configure() will break the didRegisterForRemoteNotificationsWithDeviceToken method.
You need to add GoogleUtilitiesAppDelegateProxyEnabled to the info.plist and set it to NO.

Silent APN Notification not working for Firebase IOS Auth

I'm currently trying to implementation Auth for iOS using Firebase. I've gotten it to work using the recaptcha method specified here: https://firebase.google.com/docs/auth/ios/phone-auth but still running into issues with the silent APN. I uploaded my APN key to the firebase project and added push notification to my swift project capabilities. I also added the following piece of code to my AppNameApp.swift file:
class Appdelegate : NSObject,UIApplicationDelegate{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
UIApplication.shared.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
}
}
I'm still unable to get it to work and I've been stuck for a few days now. Any help will be greatly appreciated!

IOS doesn't receive first notification in background

IOS 13.7 IPhone Xr, Xcode Version 11.7 (11E801a)
I edited app scheme and set launch mode to "Wait for the executable to be launched". I run app, and send a simple notification on my device.
this notification:
{
"aps":{
"content-available":1
}
}
in xcode status changed from "Waiting to attach to test on iPhone" to "Running test on iPhone", but the notification wasn't received.
i try catch notification in this method:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
Next notification I receive successfully. This case can be repeated after application reload.
Can you help me find out why I don't receive the first notification
Please try this payload :
{"aps":{"alert":"","content-available":1}}
If your app wasn’t running, the push notification is passed to your AppDelegate in the launchOptions
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
...
let notificationOption = launchOptions?[.remoteNotification]
if let notification = notificationOption as? [String:AnyObject]{
// received notification
}
...
}

Push notification cheat inside app

I want to create cheat for the tester to check the push notification. I have a working example from a web link and it's working. I hit the same web-link inside the app and received the success response but no notification. The Same thing is working from a web browser.
eg. I have API suppose www.example.com/send/notif/44 to send the notification to my iPhone 6 and I am calling it from my app dashboard screen notify button.
Please help.
You have to initialize 3 methods to receive remote notifications.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
<#code#>
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
<#code#>
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
<#code#>
}
If app is in foreground, it still receives push notification but not any banner is displayed by the OS. So, a tester should go to background immediately when they push that test button.

Make a web request after receiving a push notification

I have a Swift 2 iOS8+ app where I need to make a request to fetch JSON data when my app receives a push notification.
When the user clicks on the notification the app will go and fetch the data but I really need the data to be fetched as soon as the notification is received. This looks to be possible, is this the case?
I've implemented:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)){
and checking the launch options in:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
I've also enabled:
background fetch
remote notifications
None of this seems to help. If this is possible I'd be grateful for any pointers/tutorials on this.
As of iOS 9, instead of a normal push, you can use silent push notifications. When your app receives a silent push, the user is not notified, but your app can perform actions based on this notification. Then, when your background actions is finished, you create a local notification for the user.
Check out this tutorial for info on how to use silent notifications:
https://www.raywenderlich.com/123862/push-notifications-tutorial

Resources