Displaying Message Body in Push Notification

空扰寡人 提交于 2020-01-25 00:42:08

问题


I'm getting follow message when I receive Push Notification and the same JSON is displayed to user too. I want only the Data to be displayed in the body when user receives the push notification rather than displaying the whole JSON.

under aps["alert"] I get this

{
   \"Type\":\"TestType\",
   \"NotificationId\":\"40\",
   \"ImageUrl\":\"\",
   \"UserId\":1,
   \"Data\":\"Testing Push Notification\"
}

How to display Data in the message body of Push Notification.


回答1:


func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

     if(application.applicationState == UIApplicationState.Active) {
            if let alertInfo = userInfo["aps"]?["alert"] as? Dictionary<String,String>{

            if let title = alertInfo["Data"]{

            }
        }
    }
}

please try to send push notification flowing format and if application staying in background then automatic showing body message :

[aps: {
        alert =     {
        body = "Testing Push Notification";
        title = "New Message";
        NotificationId= "536"
        };
        sound = default;
        }]

Update correct format For example

{
    "aps": {
        "alert": "Hello World",
        "sound": "default"
    },
    "Person": {
        "Address": "this is a test address",
        "Name": "First Name",
        "Number": "023232323233"
    }
}

iOS Push Notification custom format




回答2:


The keys in aps are pre-defined by apple. But you can send your custom keys outside aps as shown below

{
  "aps": {
    "alert": {
      "title": "Hey!🙂 Checkout my custom notification",
      "subtitle": "Custom notification subtitle",
      "body": "Description of custom notification"
    },
    "sound": "default",
    "category": "CustomPush",
    "badge": 1,
    "mutable-content": 1
  },
  "Employee": {
    "Name": "John Doe",
    "Designation": "Manager"
  }
} 

Where Employee is custom key where you can set your own data as required



来源:https://stackoverflow.com/questions/42370479/displaying-message-body-in-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!