2

I am implementing FCM push Notification.

I get notification when App is in foreground.
But I do not get Notification in the background.

In server end I have set content-available => true

On iOS 10, should I implement any other method or where do I get background notification?

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
    print("userNotificationCenter : Forground")

    completionHandler([.Alert,.Sound,.Badge])
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)
{
    //Code Todo After User presess notification
}

PHP Server Payload Code :

$payloadFeild = array(
        'registration_ids' => $gotoFcmUidVar,
        'data' => array('MsgKey'=>json_encode($fcmMsgVar)),
        'content_available' => true,
        'priority' => 'high'
    );
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
  • Have you added `Background mode - Notifications` to your app's entitlements ? – nathan Jul 22 '17 at 22:32
  • Yes I have enable Background mode - Remote Notifications – Sujay U N Jul 22 '17 at 22:35
  • Have you followed the steps in [FCM's guide](https://firebase.google.com/docs/cloud-messaging/ios/receive) ? – nathan Jul 22 '17 at 22:36
  • Yes, I followed. So I get foreground notifications and their guide talks only about foreground notification – Sujay U N Jul 22 '17 at 22:39
  • There's a similar post in their repo: [FCM did not received push notification when background mode for ios10](https://github.com/firebase/quickstart-ios/issues/194) – nathan Jul 22 '17 at 22:40
  • 1. Can you show **exactly** what the payload looks like? You can find the correct format from [here](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html). FYI you can't add badge/alert/sound for the notification in your **iOS** code.You should configure it from server! 2. You don't need to write `UNNotificationPresentationOptions.alert`. Just write `.alert` – mfaani Jul 23 '17 at 03:24
  • Additionally the 2 methods you wrote are **same**. Only that the first one is for Swift 2.x. and the second one belongs to Swift3. If you want to get a callback then to implement `application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`. For more see my other answer [here](https://stackoverflow.com/a/44705892/5175709) – mfaani Jul 23 '17 at 03:30
  • Sorry, Updated the code now – Sujay U N Jul 23 '17 at 08:30
  • 1. Do you just want to see the notification coming?! Or you want to get callback? If you want to get callback and do something in the backgorund silently then implement: `application(_:didReceiveRemoteNotification:fetchCompletionHa‌​ndler:` ? 2. If you want **see** the notification then you should see the link I sent before. Your payload should be similar to `{ "aps" : { "alert" : "You got your emails.", "badge" : 9, "sound" : "bingbong.aiff" }, "item1" : "bar", "item2" : 42 }` – mfaani Jul 23 '17 at 11:47

1 Answers1

2

I finally got this fixed after struggle 2 months by adding 2 things :
This is missing in all documentation and is to help all whom I wish not to struggle like me

|*| Try 1) : For Ios 10 background notification, Add following deligate method in AppDeligate :

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
    application.registerForRemoteNotifications()
}

Got this solution from : https://codentrick.com/firebase-cloud-messaging-ios/


|*| Try 2) : If using Firebase : Set a value in "info.plist"
Property List View :

FirebaseAppDelegateProxyEnabled -> NO

Source Code View :

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>

I Had only mentioned FirebaseAppDelegateProxyEnable. So be careful of spelling too.

Sujay U N
  • 4,974
  • 11
  • 52
  • 88