Creating a Push Message
With Magnet Message it is easy to implement push notifications into your apps.
- Please visit Set up APNS for instructions on how to enable your app to use push notifications.
- Creating your First App contains instructions for initializing Magnet Message.
Once Magnet Message has been initialized and APNS has been set up, push notifications can be sent to recipients (MMUsers) using a MMXPushMessage object. Recipients will receive the notifications on all of their devices they have enabled. A recipient can have multiple devices, Android, iOS, or both. Magnet Message will seamlessly send notifications to the correct devices.
Example Push 1:
//retrieve user(s)
MMUser.usersWithUserNames(["SOME_USER_NAME"], success: { users in
if let user = users.first {
//create message
let msg : MMXPushMessage = MMXPushMessage.pushMessageWithRecipient(user, body: "Hello World!")
//send push
msg.sendPushMessage({ () in
NSLog("Success")
}, failure: { error in
NSLog("Error sending push %@", error.localizedDescription)
})
}
}, failure: { error in
NSLog("Error retreiving user(s) %@", error.localizedDescription)
})
Example Push 2:
//retrieve user(s)
MMUser.usersWithUserNames(["SOME_USER_NAME"], success: { users in
if let user = users.first {
//create message
let msg : MMXPushMessage = MMXPushMessage.pushMessageWithRecipient(user,
body: "Hello World",
title: "New Message",
sound: "default",
badge: 10,
userDefinedObjects: ["UserDefineKey1" : "UserDefineValue1","UserDefineKey2" : "UserDefineValue2"])
//send push
msg .sendPushMessage({ () in
NSLog("Success")
}, failure: { error in
NSLog("Error sending push %@", error.localizedDescription)
})
}
}, failure: { error in
NSLog("Error retreiving user(s) %@", error.localizedDescription)
})
Receive Push Messages
MMXPushMessage provides a simple way to recreate and access a push message's content from the userInfo dictionary that is returned in the App Delegate.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
//recreate message
let msg : MMXPushMessage = MMXPushMessage.init(pushUserInfo: userInfo)
let body : String = msg.body != nil ? msg.body! : ""
let title : String = msg.title != nil ? msg.title! : "No Title"
print("Body: \(body) title: \(title)\nContent Dictionary: \(msg.messageContent)")
}
Useful Links