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!