1

I want to send push notifications to the user if he gets new chat messages or by some other actions I already implemented in a activity feed. All activity messages I receive, I want to duplicate to push notifications if the user is not inside the app.

I already implemented some general stuff in app delegate.swift:

import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

// The callback to handle data message received via FCM for devices running iOS 10 or above.
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage) {
    print(remoteMessage.appData)
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    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 })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()
    UITabBar.appearance().tintColor = UIColor.black
    UIBarButtonItem.appearance().tintColor = .black
    return true
}

This Is my activity feed where I collect all activitys in my network. All This messages I want to send the user as push notification if the user is not inside the app:

 func updateView(report: ReportingModel) {
    if report.type == "post" {
        statusLabel.text = "hat einen neuen Post erstellt"
        createTime(report: report)

        guard let postId = report.objectId else { return }
        PostApi.shared.observePost(withPostId: postId, completion: { (post) in
            guard let postImageUrlSting = post.imageURL else { return }
            guard let imageUrl = URL(string: postImageUrlSting) else { return }
            self.postImageView.sd_setImage(with: imageUrl, completed: { (_, _, _, _) in
            })
        })
    } else if report.type == "comment" {
        statusLabel.text = "hat einen neuen Kommentar erstellt"
        createTime(report: report)

        guard let postId = report.objectId else { return }
        PostApi.shared.observePost(withPostId: postId, completion: { (post) in
            guard let postImageUrlSting = post.imageURL else { return }
            guard let imageUrl = URL(string: postImageUrlSting) else { return }
            self.postImageView.sd_setImage(with: imageUrl, completed: { (_, _, _, _) in
            })
        })
    } else if report.type == "like" {
        statusLabel.text = "hat deinen Beitrag geliked"
        createTime(report: report)

        guard let postId = report.objectId else { return }
        PostApi.shared.observePost(withPostId: postId, completion: { (post) in
            guard let postImageUrlSting = post.imageURL else { return }
            guard let imageUrl = URL(string: postImageUrlSting) else { return }
            self.postImageView.sd_setImage(with: imageUrl, completed: { (_, _, _, _) in
            })
        })
    } else if report.type == "dislike" {
        statusLabel.text = "hat deinen Beitrag gedisliked"
        createTime(report: report)

        guard let postId = report.objectId else { return }
        PostApi.shared.observePost(withPostId: postId, completion: { (post) in
            guard let postImageUrlSting = post.imageURL else { return }
            guard let imageUrl = URL(string: postImageUrlSting) else { return }
            self.postImageView.sd_setImage(with: imageUrl, completed: { (_, _, _, _) in
            })
        })
    }
}

But how to connect the activity feed to my push notifications? I now receive some manually send push notifications through cloud messaging.

Thanks in advance for your help!

jo1995
  • 414
  • 1
  • 5
  • 14
  • check out https://stackoverflow.com/questions/37899712/fcm-background-notifications-not-working-in-ios/43854631#43854631 for some code (it is a question I answered that also has some info you will likely want later... :) – Apps-n-Add-Ons Dec 28 '18 at 13:52
  • Thanks for your help! But where to go from here? I now can send notifications manually to all users. But how to generate push notifications automatically if a other user send me a message or someone liked my picture? – jo1995 Dec 28 '18 at 14:05
  • Well, they don't happen 'automatically'..... - somewhere you have to GENERATE them! I.e., you need some sort of server that receives the "message" or "like" {both of these are 'bits of data' - you may handle them differently, but they are still just 'data' to the push notification} and processed - probably you want to store the data later as well, etc...... - a server process handles all that for you (well, you have to write the code to suit your needs, but it is on a server somewhere!) – Apps-n-Add-Ons Dec 28 '18 at 14:10
  • and what 'activity feed' do you mean? how are you reading this? That is where you would put additional logic to capture the 'activity' and process it - sending it to your phone, etc...... – Apps-n-Add-Ons Dec 28 '18 at 14:17
  • edited my question. Thats exactly what I want to know. How to configure the push notifications. I`m working on a social network, so all interactions I collect in a activity feet e.g. likes, comments, post creating, text messages... – jo1995 Dec 28 '18 at 14:23
  • I see that.... - though it looks like this code is inside the app - so notifying OUTSIDE the app is tricky..... the app isn't running the 'activity feed'... - - you can receive notifications that come from outside the app, but I'm not sure if/how to keep that 'feed reader' going when the app isn't running...... I would do it from a server (external reader, sees the user and type - sends push notification) - perhaps someone else can help you with running a feed outside a running app (I did a lot with push in one app that got cancelled, so not a great expert in all things swift....) – Apps-n-Add-Ons Dec 28 '18 at 14:27

0 Answers0