I am trying to set up push notification in me Swift app and added
backendless.messaging.registerForRemoteNotifications() in the AppDelegate together with the two didRegister… and didFail… functions. It worked the first couple of times, but now suddenly it crashes the app; Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: '-[Fault copyWithZone:]: unrecognized selector sent to instance
The crash is in the didRegister… function. Should the .register… only be called once, say on first run?
Secondly, when I had it running I didn’t seem to be able to send anything from the console.
I could see the device, but sending a test message didn’t result in any message on the device, and no message in the list. However, I’m not sure what goes in the PublisherId, Subtopic and Headers fields and left them blank.
Running from Backendless-ios-SDK 3.0.15
Hi Jorgen,
You should investigate our PushNotify sample - how it is organized.
Please pay your attention to OLD marker (it marks the differences between old and new implementation) and new device registration method.
Regards,
Slava
You don’t happen to have a Swift version of that by any chance?
Sure, Jorgen
I just created a very small swift sample which publishes one message.
You could try it (with my appId & secretKey, if you will), it works fine for me - let me know how it goes for you. If you will try it with your backendless app (with your appId & secretKey), you should change bundleId to one for the your certificate .p12.
Regards,
Slava
Here is simple implementation:
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let APP_ID = "YOUR_APP_ID"
let SECRET_KEY = "YOUR_SECRET_KEY"
let VERSION_NUM = "v1"
var backendless = Backendless.sharedInstance()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
return true
}
func applicationWillTerminate(application: UIApplication) {
backendless.messaging.applicationWillTerminate()
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
backendless.messaging.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
backendless.messaging.didFailToRegisterForRemoteNotificationsWithError(error)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
backendless.messaging.didReceiveRemoteNotification(userInfo)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
backendless.messaging.didReceiveRemoteNotification(userInfo)
completionHandler(.NewData)
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController, IBEPushReceiver {
var backendless = Backendless.sharedInstance()
override func viewDidLoad() {
super.viewDidLoad()
// [url=https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/c/tdef/UIUserNotificationType]https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/c/tdef/UIUserNotificationType[/url]
backendless.messaging.notificationTypes = UIUserNotificationType.Alert.rawValue|UIUserNotificationType.Sound.rawValue
backendless.messaging.registerDeviceWithChannels(["testing"])
backendless.messaging.pushReceiver = self
}
func publish() {
print("PUBLISHED")
let publishOptions = PublishOptions()
publishOptions.assignHeaders(["publisher_name":"Tester", "ios-badge":"1", "ios-sound":"Sound12.aif", "ios-content-available":"1"])
let deliveryOptions = DeliveryOptions()
deliveryOptions.pushPolicy(PUSH_ONLY)
deliveryOptions.pushBroadcast(FOR_ANDROID.rawValue|FOR_IOS.rawValue)
backendless.messaging.publish(
"testing",
message: "ONE",
publishOptions: publishOptions,
deliveryOptions: deliveryOptions,
response: {(status: MessageStatus!) -> () in
print("Message has been sent: \(status)")
},
error: { (fault : Fault!) -> () in
print("Server reported an error: \(fault)")
})
}
// IBEPushReceiver Methods
func didReceiveRemoteNotification(notification: String!, headers: [NSObject : AnyObject]!) {
print("RECEIVED: \(headers["publisher_name"]) -> \(notification)")
}
func didRegisterForRemoteNotificationsWithDeviceId(deviceId: String!, fault: Fault!) {
if fault == nil {
print("didRegisterForRemoteNotificationsWithDeviceId: \(deviceId)")
publish()
}
else {
print("didRegisterForRemoteNotificationsWithDeviceId: \(fault)")
}
}
func didFailToRegisterForRemoteNotificationsWithError(err: NSError!) {
print("didFailToRegisterForRemoteNotificationsWithError: \(err)")
}
}