Reputation: 333
I send push notifications to my client application with a cloud script and on Android the notification looks like this:
Welcome message
Hello
I want to show the notification identically on iOS, but on my iOS device it looks like this:
Hello
Hello
How can I show the correct title("Welcome message") in my push notification on iOS?
UPDATE: I found out that my message title("Welcome message") is not in userInfo
, just the message body("Hello"). Why is the message title("Welcome message")not included in userInfo
?
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);
}
}
}
}
Upvotes: 0
Views: 1870
Reputation: 178
The key "alert" in your push payload is read by the OS as the title of the notification. If you want to show any custom title to your notification there are a few possible ways
Easy Way
Change the value of payload key "alert" to the your custom message.
Hard Way
Create Notification service extension.
It’s a small chunk of code that is run just before the phone shows a notification you’ve sent, and it lets you customise the content of that notification before the user ever sees it
- (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
}}
NOTE : you need to have the key "mutable_content" and value 1 in your payload
"aps" : {
"alert" : "Welcome Message"
"badge" : 5
"muatble_content" : 1
},
Upvotes: 1