如何在iOS推送通知中添加标题?

时间:2019-07-25 11:38:08

标签: c# xamarin.ios

我使用云脚本将推送通知发送到客户端应用程序,而在Android上,通知如下所示:

  

欢迎消息

     

你好

我想在iOS上显示相同的通知,但是在我的iOS设备上,它看起来像这样:

  

你好

     

你好

如何在iOS上的推送通知中显示正确的标题(“欢迎消息”)?

更新:我发现我的消息标题(“ Welcome message”)不在userInfo中,而只是消息正文(“ Hello”)。为什么userInfo中不包含消息标题(“ Welcome message”)?

enter image description here

    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
    {
        ProcessNotification(userInfo, false);
    }
    void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
    {
        // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
        if (null != options && options.ContainsKey(new NSString("aps")))
        {
            //Get the aps dictionary
            NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;

            string alert = string.Empty;

            if (aps.ContainsKey(new NSString("alert")))
                alert = (aps[new NSString("alert")] as NSString).ToString();

            if (!fromFinishedLaunching)
            {
                //Manually show an alert
                if (!string.IsNullOrEmpty(alert))
                {
                    NSString alertKey = new NSString("alert");
                    UILocalNotification notification = new UILocalNotification();
                    notification.FireDate = NSDate.FromTimeIntervalSinceNow(10);
                    notification.AlertTitle = aps.ObjectForKey(alertKey) as NSString;
                    notification.AlertBody = aps.ObjectForKey(alertKey) as NSString;
                    notification.TimeZone = NSTimeZone.DefaultTimeZone;
                    notification.SoundName = UILocalNotification.DefaultSoundName;
                    UIApplication.SharedApplication.ScheduleLocalNotification(notification);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

操作系统将读取推送有效载荷中的键“警报”作为通知的标题。 如果您想在通知中显示任何自定义标题,则有几种可能的方法

简便方式

将有效负载密钥“警报”的值更改为您的自定义消息。

硬路

创建通知服务扩展。

这是一小段代码,仅在手机显示您发送的通知之前运行,并且可以让您自定义该通知的内容,直到用户看到它为止

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
           withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler;

- (void)serviceExtensionTimeWillExpire;


class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?

override func didReceive(
    _ request: UNNotificationRequest,
    withContentHandler contentHandler:
        @escaping (UNNotificationContent) -> Void)
{

    print (request.content.userInfo)
    self.bestAttemptContent = request.content
    //We can now change title 
    self.bestAttemptContent.title = "Welcome message"
    contentHandler (self.bestAttemptContent)
    return
}}

注意: 您需要在有效负载中包含键“ mutable_content”和值1

"aps" : {
    "alert" : "Welcome Message"
    "badge" : 5
    "muatble_content" : 1
},