Swift - Firebase - applicationReceivedRemoteMessage Conflict - ios

Using Swift 2.3 - Firebase 4
|*| If I try to implement this method its says :
func applicationReceivedRemoteMessage(remoteMessage: MessagingRemoteMessage)
{
print("%#", remoteMessage.appData)
}
Objective-C method 'applicationReceivedRemoteMessage:' provided by method 'applicationReceivedRemoteMessage' conflicts with optional requirement method 'application(received:)' in protocol 'MessagingDelegate'
Kindly let me know which is the new correct method

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Let FCM know about the message for analytics etc.
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
// handle your message
}
reference: https://firebase.google.com/docs/cloud-messaging/ios/receive

Implement following delegate methods in your AppDelegate.swift file:
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Remote message received for this app")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("This \(userInfo)")
Messaging.messaging().appDidReceiveMessage(userInfo)
}

Related

Flutter plugin with Firebase messaging callbacks not called

I'm developing native platform Flutter plugin and I'm trying to get push notifications (through Firebase messaging) working with it but didReceiveRemoteNotification in the plugin never gets called.
I've added addApplicationDelegate to my AppDelegate and I'm getting didReceiveRemoteNotification triggered in AppDelegate but not in the plugin. However I do get didRegisterForRemoteNotificationsWithDeviceToken callbacks in both AppDelegate and plugin. I'm using Firebase messaging which might be messing up with this so I'm wondering if anybody tried this or might know what is causing callback not being called.
Plugin:
public class SomePlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
...
// Add application delegate so that we can receive AppDelegate callbacks
registrar.addApplicationDelegate(instance)
}
}
extension SomePlugin {
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NSLog("****/ didRegisterForRemoteNotificationsWithDeviceToken")
}
public func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
NSLog("****/ didReceiveRemoteNotification")
}
public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) -> Bool {
NSLog("****/ didReceiveRemoteNotification")
return true
}
}
AppDelegate:
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self as UNUserNotificationCenterDelegate
// set the type as sound or badge
center.requestAuthorization(options: [.sound,.alert,.badge, .providesAppNotificationSettings]) { (granted, error) in
// Enable or disable features based on authorization
}
application.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
extension AppDelegate {
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NSLog("**** didRegisterForRemoteNotificationsWithDeviceToken")
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
override func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.newData)
super.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}
}
extension AppDelegate {
// when user opens the notification
override func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
NSLog("**** userNotificationCenter: did receive!!!!")
completionHandler()
super.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
// app is in foreground
override func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("**** userNotificationCenter: will present")
completionHandler(UNNotificationPresentationOptions.sound)
super.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
}
}

ios FCM messaging didReceiveRemoteMessage not called

I want to use FCM to send notifications and data messages to iOS Devices.
All works fine, but for some reason the delegate method messaging didReceiveRemoteMessage is never called. Therefore I cannot get data messages when the app is in the foreground... I tried it with and without a notification block beside the data. So the message I am sending looks like this:
{'message': {
'token': 'mytokenstandshere',
'notification': {
'body': 'message',
'title': 'title'},
'data': {
'datafield': 'datavalue',
'datafield2': 'datavalue2'}
}}
I tried all possibilities (without notification, without data, with both). Notification is working without problems, but the data block is not appearing in clean data messages.
I just want to have this running on iOS 11+. But I even tried it with the parts for iOS 9 and 10 from the docs from google.
This is my AppDelegate:
#
UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Hardware.start()
Preferences.initDefaults()
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("new Token: \(fcmToken)")
Messaging.messaging().subscribe(toTopic: TOPIC_ALL_MESSAGES)
NotificationSettingsListener.start()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Got a remote")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.newData)
}
}
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print full message.
print(userInfo)
completionHandler()
}
}
I'm not seeing:
application(_: didReceiveRemoteNotification: fetchCompletionHandler:)
I believe that's required for handling. Reference
Please make sure you have to assign Firebase messaging delegate in AppDelegate.m
[FIRMessaging messaging].delegate = self;

Swift - Firebase 4 - Proper method to handle remoteMessage

Everything was working good in Firebase v3.
I recently upgraded to Firebase v4.
I implemented all the protocol functions.
Stil Received msg is not handled and I get below warning
[Firebase/Messaging][I-FCM002019] FIRMessaging received data-message, but FIRMessagingDelegate's-messaging:didReceiveMessage: not implemented
func application(appPsgVar: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
let remoteMessage = userInfo
print("Forground : remoteMessage : \(remoteMessage)")
Messaging.messaging().appDidReceiveMessage(remoteMessage)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
let remoteMessage = userInfo
print("Background : remoteMessage : \(remoteMessage)")
Messaging.messaging().appDidReceiveMessage(remoteMessage)
completionHandler(UIBackgroundFetchResult.NewData)
}
// Fcm Message Received Handler Functions :
func application(received remoteMessage: MessagingRemoteMessage)
{
// What message comes here?
print("remoteMessage.appData : ", remoteMessage.appData)
}
// older way
func application(remoteMessage: MessagingRemoteMessage)
{
print("remoteMessage.appData : ", remoteMessage.appData)
}
// Fcm Message protocol Functions :
func messaging(messaging: Messaging, didRefreshRegistrationToken fcmToken: String)
{
print("New FCM Token received : \(fcmToken)")
}
#available(iOS 10.0, *)
func messaging(messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage)
{
// What message comes here ?
print("remoteMessage.appData : ", remoteMessage.appData)
}
|*| If I try to implement this method its says :
func applicationReceivedRemoteMessage(remoteMessage: MessagingRemoteMessage)
{
print("%#", remoteMessage.appData)
}
Objective-C method 'applicationReceivedRemoteMessage:' provided by method 'applicationReceivedRemoteMessage' conflicts with optional requirement method 'application(received:)' in protocol 'MessagingDelegate'
Kindly let me know which is the new correct method

Why I'm not able to handle received push notifications in iOS 10?

I already imported
import UserNotifications
and connected delegate:
UNUserNotificationCenterDelegate
and set delegates as self:
UNUserNotificationCenter.currentNotificationCenter().delegate = self
FIRMessaging.messaging().remoteMessageDelegate = self
and finally I have these functions:
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
print("User Info = ",notification.request.content.userInfo)
completionHandler([.Alert, .Badge, .Sound])
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
//Handle the notification
print("User Info = ",response.notification.request.content.userInfo)
completionHandler()
}
but I do not get on my log any prints when I'm testing on a real device and in the background mode.
What is the problem?
internal func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print(userInfo)
}
Please try with following code. You just need to save data.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> ())
{
let info : NSDictionary! = userInfo as! NSDictionary
if info != nil
{
let aps = info["aps"] as? NSDictionary
UserDefaults.standard.set(aps, forKey: "aps")
}
}
And use the user defaults where you want to use.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let info = userInfo as! [String : AnyObject]
let aps = info["aps"] as? NSDictionary
print(aps)
}

Swift didReceiveRemoteNotification not called

I have an app with oneSignal as push provider. I can receive push notifications, that work good. But if I try to access push payload I get nothing as didReceiveRemoteNotification not called.
I have following code
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if application.applicationState != UIApplicationState.Background {
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var pushPayload = false
if let options = launchOptions {
pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
}
}
if application.respondsToSelector("registerUserNotificationSettings:") {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
application.registerForRemoteNotificationTypes(types)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if userInfo["d"] as! String == "t" {
print("key was received")
}
print(userInfo)
print("PREVED")
}
Problem is that nothing prints out when I receive push. What am I doing wrong ?
try once your delegete method in this place
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
call this one
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("Recived: \(userInfo)")
completionHandler(.NewData)
}
Swift 4.2 - call this
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
debugPrint("Received: \(userInfo)")
completionHandler(.newData)
}
Use this code for Swift 3.0
//MARK: Push notification receive delegates
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
{
print("Recived: \(userInfo)")
completionHandler(.newData)
}
Hey #Alexey make sure your app provisioning file has enabled for Push Notification Service
Pretty old post but solutions can be found here in my post Push Notifications are delivered but didReceiveRemoteNotification is never called Swift or here didReceiveRemoteNotification function doesn't called with FCM notification server.
Check for this parameter: "content_available": truein your notification definition or push notification service. With underscore is how it should be set.

Resources