I'm trying to debug why I can't get messages from firebase on my iPhone,
I am connected to the FCM server, and I've also subscribed to a topic, however, when I try to print data recieved from FCM I get nil values.
This is the code I have from google's tutorial, it gets executed so that does mean the app is being notified correctly that a new topic is available on FCM?
Also how can I debug to see if there's something I'm doing wrong in setting FCM up?
Thanks
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
FIRMessaging.messaging().subscribe(toTopic: "/topics/newNotificationtest")
print("Message ID: \(userInfo["gcm.message_id"])")
debugPrint(userInfo)
// Print full message.
print("%#", userInfo)
}
Please Try With this code and debug
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
let aps = userInfo["aps"] as? NSDictionary
print(aps)
print(userInfo)
print("Message ID: \(userInfo["gcm.message_id"]!)")
print("%#", userInfo)
}
Related
I'm trying to listen user clear push notification action. I've the code below, It's working when the app is on the foreground but when i put the app on the background it doesn't go into that function. Is there any way that i can do some coding when the user clear the notification on the background state?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if(response.actionIdentifier == UNNotificationDismissActionIdentifier){
...
//I need to do some coding here!
}
}
I use this for receiving remote notifications behind the scenes:
extension AppDelegate: UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// Process this userInfo dictionary
processNotification(dictionary: userInfo)
}
In my willFinishLaunchingWithOptions in AppDelegate I call this:
fileprivate func setupAPN(application: UIApplication) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
MobilePlatformPush.setRemoteDelegate(delegate: self)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
application.registerForRemoteNotifications()
}
I need to get local notification data from lock screen or apps was kill. Is there a way to detect it or any handler that would trigger after show?.
i try using UNUserNotificationCenter present and UNUserNotificationCenter didReceive but still not working..
response.notification.request.content.userInfo contains the notification data
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
completionHandler()
guard let bodyText = response.notification.request.content.userInfo["body"] as? String else {return}
}
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any])
{
if application.applicationState == .inactive || application.applicationState == .background
{
//opened from a push notification when the app was on background
}
}
Here are the details of the error when app throw error to register the notification:
<EXPR>:3:1: error: expected member name or constructor call after type name
Error
^
<EXPR>:3:1: note: use '.self' to reference the type object
Error
^
.self
Here is the code which is running successfully in iOS 10.1
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
print("Failed to register Notification Error = ", Error)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
print("Device Token = ", Data)
}
func registerForRemoteNotification()
{
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge])
{ (granted, error) in
if error == nil
{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else
{
if #available(iOS 8.0, *)
{
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
else
{
UIApplication.shared.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
}
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
{
if let dicAps = notification.request.content.userInfo["aps"] as? NSDictionary
{
AppUtilities.showAlertWithMessage(title: APP_TITLE as NSString, message: "\(dicAps.object(forKey: "msg")!)" as NSString)
}
completionHandler([.alert, .badge, .sound])
}
//Called to let your app know which action was selected by the user for a given notification.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
{
print("User Info = ",response.notification.request.content.userInfo)
}
I am trying to implement an app which will run on iOS 8 to 10.1., it works fine in iPhone 5s with iOS 10.1.1. but when I run the app in the simulator with iOS 8.1, it always throws an error which is described above, please correct me if I missed anything.
Thank you.
Sorry to say, but you'll need to find some hardware to test this functionality.
Push notifications are not available in the simulator. They require a provisioning profile from iTunes Connect, and thus are required to be installed on a device. That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $99.
Source : How can I test Apple Push Notification Service without an iPhone?
I made a lot of search during many days (20 posts+) but I didn't the solution to my problem.
I have an application which receive push notification, I can receive those remote notifications but when I tap on it, the delegate "didReceiveRemoteNotification" is not called (I want to intercept the payload to do action in fonction of it).
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print("test")
}
In advance, thanks for help.
The didReceiveRemoteNotification method does not fire when the user launches the app by tapping on the notification and not when the notification is received, never.
Sorry for my bad english and if you have question, don't hesitate.
First import UserNotification framework and then implement UNUserNotificationCenterDelegate in ios10 to support push notification in ios10
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
//Handle notification tap
}
How do I set up my AppDelegate to handle push notifications that occur when the app is in the foreground and in the background with swift 3 and ios 10? Including how to make the phone vibrate while in the foreground if I receive a notifcation.
Here is how I set up my AppDelegate file to do this:
To handle push notifications, import the following framework:
import UserNotifications
To make the phone vibrate on any device import the following framework:
import AudioToolbox
Make your AppDelegate a UNUserNotificationCenterDelegate:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
In your "didFinishLaunchingWithOptions" add this:
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted) {
UIApplication.shared.registerForRemoteNotifications()
} else{
print("Notification permissions not granted")
}
})
This will determine if the user has previously said that your app can send notifications. If not, handle it how you please.
To get access to the device token once it is registered:
//Completed registering for notifications. Store the device token to be saved later
func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) {
self.deviceTokenString = deviceToken.hexString
}
hexString is an extension I added to my project:
extension Data {
var hexString: String {
return map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
}
}
To handle what happens when your app receives a notification in the foreground:
//Called when a notification is delivered to a foreground app.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
//This will get the text sent in your notification
let body = notification.request.content.body
//This works for iphone 7 and above using haptic feedback
let feedbackGenerator = UINotificationFeedbackGenerator()
feedbackGenerator.notificationOccurred(.success)
//This works for all devices. Choose one or the other.
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
}
To handle what happens when a users presses on a notification they receive (from your application) while your app is in the background, call the following function:
//Called when a notification is interacted with for a background app.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
//Handle the notification
print("did receive")
let body = response.notification.request.content.body
completionHandler()
}
I would add to havak5's answer that you can set push notifications to be shown as iOS default push, just like this:
swift code:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
if UIApplication.shared.applicationState == .active {
completionHandler( [.alert,.sound]) // completionHandler will show alert and sound from foreground app, just like a push that is shown from background app
}
}