Adding push notification to exist App

Dear Sir,

I have an App published in the store without push notification, now I want to update a new version for this App to include thus feature. I searched in the net and all the articles I found talks about how to setup a new App that includes Push notification, but I need to modify an exist App to include Push notification.

thanks
Samer

Hi @samerhameed, it basic the same to add Push Notification in new app or existed one.

In the AppDelegate you add the methods for the push (registerForRemoteNotifications and the other)
You need to select in your code where to add the registration to the push.

In the apple member center https://developer.apple.com you can skip the creating of new app and go right to: select Certificates, IDs & Profiles → Identifiers → App IDs. Than select your app, than Edit than you will see the Push Notifications section to add your new certificate.

You can read the tutorial Push Notifications Tutorial: Getting Started for more reference.

Dear Sir,

I used the below code for delegate

//
// AppDelegate.swift
// FireBaseTest
//
// Created by Admin on 9/23/17.
// Copyright © 2017 Admin. All rights reserved.
//

import UIKit
import UserNotifications
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let gcmMessageIDKey = "gcm.message_id"

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    FIRApp.configure()
    
    // [START set_messaging_delegate]
    FIRMessaging.messaging().remoteMessageDelegate = self
    // [END set_messaging_delegate]
    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    
    application.registerForRemoteNotifications()
    
    // [END register_for_notifications]
    return true
}

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    
    // Print full message.
    print(userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    
    // Print full message.
    print(userInfo)
    
    completionHandler(UIBackgroundFetchResult.newData)
}
// [END receive_message]
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Unable to register for remote notifications: \(error.localizedDescription)")
}

// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the FCM registration token.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
    print("APNs token retrieved: \(deviceToken)")
    // With swizzling disabled you must set the APNs token here.
    // Messaging.messaging().apnsToken = deviceToken
}

}

// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    
    // Print full message.
    print(userInfo)
    
    // Change this to your preferred presentation option
    completionHandler([.alert,.badge,.sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }
    
    // Print full message.
    print(userInfo)
    
    completionHandler()
}

}
// [END ios_10_message_handling]

extension AppDelegate : FIRMessagingDelegate {

//this method for recieve remote message data

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    // here we parse coming data
    print(remoteMessage.appData)
}

// [START refresh_token]
func messaging(_ messaging: FIRMessaging, didRefreshRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}
// [END refresh_token]
// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: FIRMessaging, didReceive remoteMessage: FIRMessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")
}
// [END ios_10_data_message]

}

but a notification appears to me says.

Replace ‘FIRApp’ with ‘FirebaseApp’ in all the delegate code

and I did, but I can not receive any notification.

below is my pod setting

Uncomment the next line to define a global platform for your project

platform :ios, ‘10.0’

target ‘School’ do

Comment the next line if you’re not using Swift and don’t want to use dynamic frameworks

use_frameworks!

Pods for School

pod ‘Firebase/Core’
pod ‘Firebase/Messaging’
end

Regards

Dear Sir,

below link contains the project code, I hope you can check it and support me to find the problem

https://www.4shared.com/zip/ZCddgVvxca/xxxx.html?

Regards
Samer

Hi again @samerhameed

Replace ‘FIRApp’ with ‘FirebaseApp’ in all the delegate code

If I remember correctly, they change the api of Firebase, and i’m not sure if they update the documentation (I add this issue a few months ago, and I never checked this again).

I think the reason you don’t getting notification is because you skip some step when you impalement the Push Notification. Like enabling it in the Capabilities tab in your target. But maybe it is also some other steps you need to do.

Usually when I learning something new, and I need to implement it in a existed project, I usually do it this way:

  • Maybe do a tutorial for creating new app with push notification
  • Using version control to commit each of the following steps (If you don’t know version control like git, than make backup for the project):
    • Create simple new app that receive push notification (it will print to console, or show alert with the notification).
    • Add Firebase to the project with a new test app (step by step using there Tutorial) with testing account/ app.
    • Update notification system to use Firebase (Check the push notification is still working).
    • Move to the original project/ app and add the functionality step by step (Check the push notification is still working).
    • Update the Firebase’s credential to use the correct app/ account

Dear Sir,

thank you for your help and support. issue solved

Regards

This topic was automatically closed after 166 days. New replies are no longer allowed.