Despite having "remote notification" in background modes, the app does not seem to do anything when the app is in the background. All I want to do is updating the badge number when the app is closed or terminated (like what Twitter app does).
func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
UIApplication.shared.applicationIconBadgeNumber = 99 // just to test
completionHandler(.newData)
}
Background Mode settings:
When the app is in foreground it updates the badge, this is the payload:
{
"aps": {
"badge": 4,
"content-available": 1,
"mutable-content": 0
}
}
But when it's in background it does nothing. Should it not update the badge?
Related
I'm trying to listen user clear push notification action. I've the code below, It's working when the app is on the foreground but when i put the app on the background it doesn't go into that function. Is there any way that i can do some coding when the user clear the notification on the background state?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if(response.actionIdentifier == UNNotificationDismissActionIdentifier){
...
//I need to do some coding here!
}
}
I use this for receiving remote notifications behind the scenes:
extension AppDelegate: UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// Process this userInfo dictionary
processNotification(dictionary: userInfo)
}
In my willFinishLaunchingWithOptions in AppDelegate I call this:
fileprivate func setupAPN(application: UIApplication) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
MobilePlatformPush.setRemoteDelegate(delegate: self)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
application.registerForRemoteNotifications()
}
I am using FCM for push notifications. I used to refresh the content of the app using the below method
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Now with iOS13 this method is not firing anymore. I have included apns-push-type and apns-priority as well.
I found the issue.
UIApplication.shared.registerForRemoteNotifications()
this has to run for every launch. Better to keep this in didFinishLoadingWithOptions method. In my previous version, I used to call this for the first time but looks like it has to be for every launch.
and make sure set delegates for notification and messaging also.
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if #available(iOS 13.0, *) {
// your code
completionHandler(UIBackgroundFetchResult.newData)
} else {
}
}
also check your APNS certificate on FCM, it must .p12 type must be available in keychain that you are using for your current XCode version
I made a lot of search during many days (20 posts+) but I didn't the solution to my problem.
I have an application which receive push notification, I can receive those remote notifications but when I tap on it, the delegate "didReceiveRemoteNotification" is not called (I want to intercept the payload to do action in fonction of it).
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("test")
}
In advance, thanks for help.
The didReceiveRemoteNotification method does not fire when the user launches the app by tapping on the notification and not when the notification is received, never.
Sorry for my bad english and if you have question, don't hesitate.
First import UserNotification framework and then implement UNUserNotificationCenterDelegate in ios10 to support push notification in ios10
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
//Handle notification tap
}
I have managed to schedule a notification and show it to the user when the app is running/not running in the foreground. Now I need to display a ViewController upon the tap of this notification.
I understand that didReceiveRemoteNotification is the function that is called when the user taps on a notification. In my case, This function is never fired.
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
//didReceiveRemoteNotification goes here
The didReceiveRemoteNotification function:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if ( application.applicationState == UIApplicationState.active)
{
print("Active")
// App is foreground and notification is recieved,
// Show a alert.
}
else if( application.applicationState == UIApplicationState.background)
{
print("Background")
// App is in background and notification is received,
// You can fetch required data here don't do anything with UI.
}
else if( application.applicationState == UIApplicationState.inactive)
{
print("Inactive")
// App came in foreground by used clicking on notification,
// Use userinfo for redirecting to specific view controller.
}
}
This is the entire Notification related code in my AppDelegate. Am I missing something?
For UserNotifications framework you need to work with UNUserNotificationCenterDelegate, so implement UNUserNotificationCenterDelegate with AppDelegate and set the delegate in didFinishLaunchingWithOptions method.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
Now you need to implements userNotificationCenter(_:willPresent:withCompletionHandler:) and userNotificationCenter(_:didReceive:withCompletionHandler:) methods to get notification.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
//Handle notification
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
// pull out the buried userInfo dictionary
let userInfo = response.notification.request.content.userInfo
if let customData = userInfo["customData"] as? String {
print("Custom data received: \(customData)")
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
// the user swiped to unlock
print("Default identifier")
case "show":
// the user tapped our "show more info…" button
print("Show more information…")
break
default:
break
}
}
// you must call the completion handler when you're done
completionHandler()
}
You can also check this AppCoda tutorial Introduction to User Notifications Framework in iOS 10 for more details.
Below are the code I have when receive notification. I know I can use PFPush to create a alert, but I feel that it is not a good way. Is there a way to create a banner instead of alert?
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
NSNotificationCenter.defaultCenter().postNotificationName("getMessage", object: nil)
if application.applicationState == UIApplicationState.Active {
//PFPush.handlePush(userInfo)
}
completionHandler(UIBackgroundFetchResult.NewData)
}