Different APN Device Token ID for same application - swift

I just followed the tutorial of http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 for creating a sample push notification example....
But I dono where I went wrong I am receiving different token ID on different devices for the same application.
Here is my code for reference,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
return true
}
func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
println("My token is \(deviceToken)") // am getting it different for different devices
}
//Called if unable to register for APNS.
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println(error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("Recived: \(userInfo)")
//Parsing userinfo:
var temp : NSDictionary = userInfo
if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
{
var alertMsg = info["alert"] as! String
var alert: UIAlertView!
alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
}
One more problem is that I am receiving a sound and banner but not "badge"
Any help would be appreciated....

In your "didRegisterForRemoteNotificationsWithDeviceToken"
send your device Token to server like this
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let deviceTokenStr = HDDataLayer.convertDeviceTokenToStr(deviceToken)
HDDataLayer.postNotificationServiceResponseByUrlString("pushnotifications/notifications/register", andParams: tempDict, andDictCompletion: { (response: AnyObject!, error: NSError!) -> Void in
NSLog("Device Register successfully")
})
}
Please Note that HDDataLayer is my own class and you will use class you are using to interact with your server.

Related

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FBLPromise HTTPBody]

I m building a swift application. I m trying to integrate Firebase Notification message. So I write all code as documentation, but if I try to start application after some seconds, I will have this strange error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FBLPromise HTTPBody]: unrecognized selector sent to instance 0x281b9cdb0'
This is my CocoaPods file:
target 'appUser' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'TPKeyboardAvoidingSwift'
pod 'ActiveLabel'
pod 'HSAttachmentPicker'
pod 'IQKeyboardManagerSwift'
pod 'Alamofire', '~> 4.8.0'
pod 'SwiftyJSON'
pod 'DropDown'
pod 'SDWebImage/WebP'
pod 'SVProgressHUD'
pod 'Cosmos'
pod 'Gallery'
pod 'Charts'
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
# Pods for appUser
#target 'appUserTests' do
# inherit! :search_paths
# Pods for testing
#end
#target 'appUserUITests' do
# Pods for testing
#end
end
This is the code into AppDelegate.swift file
//
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,MessagingDelegate {
var window: UIWindow?
let notificationCenter = UNUserNotificationCenter.current()
/*func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}*/
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
USER_DEFAULT.set("mykey", forKey: IOS_TOKEN)
notificationCenter.delegate = self
FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().isAutoInitEnabled = true
self.configureNotification()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func configureNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
}
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
USER_DEFAULT.set(token, forKey: IOS_TOKEN)
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
// k.iosRegisterId = deviceTokenString
Messaging.messaging().apnsToken = deviceToken
print("APNs device token: \(deviceTokenString)")
// USER_DEFAULT.set(deviceTokenString, forKey: IOS_TOKEN)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("APNs registration failed: \(error)")
}
// MARK:-  Received Remote Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
print("main method - \(userInfo)")
if let info = userInfo as? Dictionary<String, AnyObject> {
//let alert1 = info["aps"]!["alert"] as! Dictionary<String, AnyObject>
let title = userInfo["gcm.notification.status"] as! String
hanleNotification(info: info, strStatus: title, strFrom: "Front")
}
completionHandler(UIBackgroundFetchResult.newData)
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
print("willPresent \(userInfo)")
Messaging.messaging().appDidReceiveMessage(userInfo)
/*if let info = userInfo as? Dictionary<String, AnyObject> {
let title = userInfo["google.c.a.c_l"] as! String
hanleNotification(info: info, strStatus: title, strFrom: "Front")
}*/
completionHandler([[.alert, .sound]])
}
//metodo chiamata quando l'utente clicca sulla notifica
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("Did Recceive \(userInfo)")
if let info = userInfo as? Dictionary<String, AnyObject> {
//let alert1 = info["aps"]!["alert"] as! Dictionary<String, AnyObject>
//let title = userInfo["gcm.notification.data"]
hanleNotification(info: info, strStatus: "titolo", strFrom: "Back")
}
//Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler()
}
/*func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.noData)
}*/
//MARK:GoViewCotroller
func hanleNotification(info:Dictionary<String,AnyObject>,strStatus:String,strFrom:String) {
let visibleVC = UIApplication.topViewController()
//recupero i dati del messaggio
var datiNascosti = info["gcm.notification.data"]
//let swiftyJsonVar = JSON(datiNascosti)
datiNascosti = datiNascosti?.replacingOccurrences(of: "\"", with: "") as AnyObject
datiNascosti = datiNascosti?.replacingOccurrences(of: "{", with: "") as AnyObject
datiNascosti = datiNascosti?.replacingOccurrences(of: "}", with: "") as AnyObject
let array:[String]? = datiNascosti?.components(separatedBy: [","])
var contentOfPushMessage = [String: String]()
for riga in array! {
//ho ogni riga adesso effettuo lo split
let rigaSlittata:[String]? = riga.components(separatedBy: [":"])
if(rigaSlittata!.count > 1){
contentOfPushMessage[rigaSlittata![0]] = rigaSlittata![1]
}
}
print("ho creato il mio dizionario con i dati ottenuti")
//se nel dizionario esiste la chiave status ed il valore = chat
//devo aprire direttamente la chat con l'utente
if contentOfPushMessage.keys.contains("status") {
// contains key
let status = contentOfPushMessage["status"]
if(status == "chat"){
//devo aprire la chat. Mi occorre anche l'id del utente
let user_id = contentOfPushMessage["user_id"]
let username = contentOfPushMessage["username"]
//apro il mio ViewController
guard let window = UIApplication.shared.keyWindow else { return }
//let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let objVC = kStoryboardMain.instantiateViewController(withIdentifier: "ChatVC") as! ChatVC
objVC.receiverId = user_id!
objVC.userName = username!
let navController = UINavigationController(rootViewController: objVC)
navController.modalPresentationStyle = .fullScreen
// you can assign your vc directly or push it in navigation stack as follows:
window.rootViewController = navController
window.makeKeyAndVisible()
}
} else {
// does not contain key
}
if strStatus == "join_live_session" || strStatus == "exist_session" || strStatus == "join_request" {
// if visibleVC is liveSessionVC {
// NotificationCenter.default.post(name: NSNotification.Name(NotificationCenterStruct.JOIN_SESSION), object: "", userInfo: nil)
// } else {
// //visibleVC?.tabBarController?.selectedIndex = 2
// }
}
}
}
I attach also an image to display Xcode when app still crashed

cannot call either didRegisterForRemoteNotificationsWithDeviceToken or didFailToRegisterForRemoteNotificationsWithError

My application cannot call didRegisterForRemoteNotificationsWithDeviceToken on iOS10 devices, even the same code can call it on iOS11 devices.
I don't know why it happening and how to solve this issue.
If anyone knows answers, please help me.
my environment is below:
Xcode: 9.2
Swift: 3.2
deployment target: 10.0
my code is below:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) {granted, error in
if error != nil {
return
}
if granted {
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = String(format: "%#", deviceToken as CVarArg) as String
let tokenWithoutSpace = token.replacingOccurrences(of: "[ |<>]", with: "", options: .regularExpression)
print(tokenWithoutSpace)
}
Thank you for your help.

why i'm getting device is not registered for apns error? i have valid certificates for development enviorment

why i'm getting device is not registered for apns error?i'm not getting device id
i have tried following to get deviceid
func application(application: UIApplication,
didFinishLaunchingWithOptions
launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
var characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
var deviceTokenString: String = (deviceToken.description as NSString)
.stringByTrimmingCharactersInSet(characterSet)
.stringByReplacingOccurrencesOfString( " ", withString: "") as String
println(deviceTokenString)
}
is it because of apple error?
Make sure you added push notification entitlement in xcode & App id is registered with push notification.
See screen shot

Push remote notification

I need create in my app a code that receive a remote information and push it to user when app is on background, I read on web that I need to use didReceiveRemoteNotification on appDelegate, to use remote push notication. I read something about and I need keys and certificates, I do not understand how to use didReceiveRemoteNotification
I need to learn about to push remote notification and how to use. I would like a tutorial or example how create it using swift 2.3.
I used this link and I found it the most helpful
http://www.appcoda.com/push-notification-ios/
I used this app for testing
https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12
This is the code I have in my AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
print(UIDevice.currentDevice().identifierForVendor!.UUIDString)
return true
}
func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("Device Token:", tokenString)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to register:", error)
}

Parse.com chek push notification before showing

How to check push notification before showing?
I want check data and if incorrect data I don't want show notification
my code:
I just use simple Parse.com example. This is my AppDelegate class
let defaults = NSUserDefaults.standardUserDefaults()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("//",
clientKey: "//")
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 (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
}
}
var notificationActionAccept :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionAccept.identifier = "FIRST_ACTION"
notificationActionAccept.title = "Įvertinti"
var notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "CallNotificationCategory"
notificationCategory .setActions([notificationActionAccept], forContext: UIUserNotificationActionContext.Default)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: NSSet(array:[notificationCategory]) as! Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
if let launchOptions = launchOptions as? [String : AnyObject] {
if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
self.application(application, didReceiveRemoteNotification: notificationDictionary)
}
}
return
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
return
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %#", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
}
}
I Realy need help:/
No, unfortunately you can not stop notification from showing when your app is in background.
application:didReceiveRemoteNotification: is just a delegate method, which is invoked upon notification showing, it can not control if notification should be shown or not.