项目作者: burakgunduztr

项目描述 :
It's a demo app to test remote APNS features: category, mutable-content, localization for alert title or body.
高级语言: Swift
项目地址: git://github.com/burakgunduztr/PushServiceDemo-Swift.git
创建时间: 2019-12-05T13:24:51Z
项目社区:https://github.com/burakgunduztr/PushServiceDemo-Swift

开源协议:

下载


PushServiceDemo-Swift

It’s a demo app to test remote APNS features: category, mutable-content, localization for alert title or body.

  • Note: Use these payloads with your desired APNS client.

Simple-Alert

  1. {"aps":{"alert":{"title":"Test push","body":"This is simple alert push."}}}

Mutable-Content

  1. {"aps":{"mutable-content":1,"alert":{"title":"Test m-content","body":"It's a test for mutable content."}}}

Category

  1. {"aps":{"category":"MEETING_INVITATION","alert":{"title":"Test category","body":"Tap to see actions about that push."}}}

Mutable-Content & Category

  1. {"aps":{"category":"MEETING_INVITATION","mutable-content":1,"alert":{"title":"Test m-content, category","body":"It's a test for mutable content and category."}}}

Alert Localization - Key/Value sets

  1. {"aps":{"alert":{"title-loc-key":"groupChatSessionInvitationNotification_title","loc-key":"groupChatSessionInvitationNotification_body","loc-args":["Friends"]}}}

Localizable.strings file on Xcode project

  1. "groupChatSessionInvitationNotification_title" = "You're invited to join a group";
  2. "groupChatSessionInvitationNotification_body" = "Group name is: %@";
  • Note: See sample clients below that you can send payloads.

Send payloads by JWT APNs Provider:(sideshow/apns2)

  • Learn how to create token from Apple
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/sideshow/apns2"
  6. "github.com/sideshow/apns2/token"
  7. )
  8. func main() {
  9. authKey, err := token.AuthKeyFromFile("________.p8")
  10. if err != nil {
  11. log.Fatal("token error:", err)
  12. }
  13. token := &token.Token{
  14. AuthKey: authKey,
  15. // KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
  16. KeyID: "________",
  17. // TeamID from developer account (View Account -> Membership)
  18. TeamID: "________",
  19. }
  20. notification := &apns2.Notification{}
  21. notification.DeviceToken = "________"
  22. notification.Topic = "________"
  23. notification.Payload = []byte(`{"aps":{"category":"MEETING_INVITATION","alert":{"title":"Test category","body":"Tap to see actions about that push."}}}`)
  24. client := apns2.NewTokenClient(token)
  25. res, err := client.Push(notification)
  26. if err != nil {
  27. log.Println("There was an error", err)
  28. return
  29. }
  30. if res.Sent() {
  31. log.Println("Sent:", res.ApnsID)
  32. } else {
  33. fmt.Printf("Not Sent: %v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
  34. }
  35. }
  • Note: You can do it with certificate too. You can check details from (sideshow/apns2)

How to enable mutable-content on iOS app:

Create Notification Service Extension:

Xcode > File > New > Target > Notification Service Extension

How to get deviceToken on iOS app:

Register remote notification and device-token handler in AppDelegate: didFinishLaunchingWithOptions

  1. let center = UNUserNotificationCenter.current()
  2. center.delegate = self
  3. center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {
  4. (granted, error) in
  5. if granted {
  6. DispatchQueue.main.async {
  7. UIApplication.shared.registerForRemoteNotifications()
  8. }
  9. }
  10. })

Add UNUserNotificationCenterDelegate to AppDelegate class, then add this method

  1. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  2. let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
  3. print("Handled deviceToken: \(token)")
  4. }

Use device token and mutable-content payload to test it.