简体   繁体   English

是否可以在iOS上修改传入的推送通知文本

[英]Is it possible to modify incoming Push Notification Text on iOS

I have a usecase where a notification provider ends up sending me push notification text in a format that I'd like to change. 我有一个用例,通知提供商最终以我想要更改的格式向我发送推送通知文本。 Can I do so in the app's ObjectiveC/Swift code? 我可以在应用程序的ObjectiveC / Swift代码中这样做吗?

Since iOS 10 , it is possible to modify the text of push notifications (unless they are silent notifications) by creating a UNNotificationServiceExtension for your application. iOS 10开始 ,可以通过为应用程序创建UNNotificationServiceExtension来修改推送通知的文本(除非它们是静默通知)。

The extension is created as a separate Target in Xcode and provides a NotificationService class with two functions, one that allows you to modify the content of the push notification ( didReceive withContentHandler ) and one that notifies your application if didReceive withContentHandler did not complete in time ( serviceExtensionTimeWillExpire ). 扩展在Xcode中作为单独的Target创建,并为NotificationService类提供两个函数,一个允许您修改推送通知的内容( didReceive withContentHandler ),另一个通知您的应用程序,如果didReceive withContentHandler没有及时完成( serviceExtensionTimeWillExpire )。

For this to work, the push notification that is sent to your application also needs to be modified to include the key/value pair mutable-content with a value of 1 so that iOS knows to invoke the UNNotificationServiceExtension for your application. 为此,还需要修改发送到应用程序的推送通知,以包含值为1的键/值对mutable-content ,以便iOS知道为您的应用程序调用UNNotificationServiceExtension。

Apple's UNNotificationServiceExtension Documentation Apple的UNNotificationServiceExtension文档

No you can't change the notification alert message(text), which user will see when app is not in foreground state. 不,您无法更改通知警报消息(文本),当应用程序未处于前台状态时,用户将看到该消息。 The use will see the same text what the notification payload contains. 该用法将看到通知有效负载包含的相同文本。

您的iOS应用程序在向用户显示之前无法修改传入的推送通知数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM