Multi line title in push notification for iOS - ios

I'm integrating FCM for an app which has both iOS and Android version.
Currently, for iOS, I want to send title in multiple lines. How do I do this? I'm trying out using Firebase console to test. But nothing seems to work for me.
I tried with these
\n
<br/>
\\n
\r\n
as suggested in this post. But nothing really worked.
I basically want the first 2 lines of the notification to be in bold. Any help much appreciated.

The 1st line in bold you see is a notification Title, the second the Subtitle, and the Content underneath (regular font).
These are properties of a notification, since iOS 10 set on UN(Mutable)NotificationContent (subtitle line is not available before).
They properties can be configured in the payload sent via APNS:
{
"aps": {
"alert": {
"title": "Custom title",
"subtitle": "Custom subtitle",
"body": "Custom message"
}
}
}
Also, you can create a UserNotificationServiceExtension to update the above properties of a notification that has been received.

Related

How to set title for iOS push notification using pushd?

In Apple iOS documentation it is mentioned that:
alert
string/dictionary
If this property is included, the system displays a standard alert or a banner, based on the user’s setting.
You can specify a string or a dictionary as the value of alert. If you specify a string, it becomes the message text of an alert with two buttons: Close and View. If the user taps View, the app launches. If you specify a dictionary, refer to Table 5-2 for descriptions of the keys of this dictionary.
title
string
A short string describing the purpose of the notification. Apple Watch displays this string as part of the notification interface. This string is displayed only briefly and should be crafted so that it can be understood quickly. This key was added in iOS 8.2.
body
string
The text of the alert message.
Now I want to set title for the push notification to be shown on iOS, I have sent the below payload:
{'data.project': {'ack': 0,
'c': {'id': 'SOME DATA'},
'ca': 1,
't': 'e/user',
'v': '1',
'vm': 2},
'msg': 'My message',
'title': 'My custom title'}
The push is sent but no title is set on the mobile device. Does pushd support it? What should I do?
Use this JSON Payload for sending push notification from backend. Hope this will help you. Here, "title" is the title of push notification and "body" is the text of alert message.
{
"registration_ids" : ["put_id_here"],
"notification" : {
"content_available" : true,
"body" : "this is body",
"title" : "this is title",
"sound" : "default",
"badge" : 1
}
}

apns-collapse-id not merging multiple notifications iOS 10

I am trying to collapse/merge multiple notifications belonging to same event in my app. iOS 10 introduced a key name apns-collapse-id in payload. Document says multiple remote notifications with same apns-collapse-id will merged and shown as single notification.
So we have the payload with apns-collapse-id and thread-id in our notification payload. Unfortunately as there is no much content online explaining how these keys actually works, for safe play we have used the same unique value for both apns-collapse-id and thread-id.
We are expecting the multiple remote notifications with same apns-collapse-id to merge. It is not happenning.
Few people suggested APNS collapse does not come for free and I have to implement UNNotificationContentExtension and handle the notifications manually. I know I can add and remove local notification already posted but not really sure how to update the already posted remote notification.
Here is how our payload looks like
payload {
"aps": {
"alert": {
"title": "Some title : ",
"body": "Some body text"
},
"sound": "default",
"payload": {
"target": {
"some key" : "Some value"
},
"thread_id": "Some_string_155863",
},
"apns-collapse-id": "Some_string_155863",
"mutable-content": 1,
"thread-id": "Some_string_155863",
"badge": 33
},
"thread-id": "Some_string_155863",
"apns-collapse-id": "Some_string_155863",
"mutable-content": 1
}
as you can see apns-collapse-id and thread-id are repeated multiple times in JSON because we aren't sure where exactly to have them
We are not using HTTPS/2 yet, but I believe iOS 10 notfication still supports legacy HTTPS. I am not really a network guy so I don't understand these things completely. Ignore my arrogance towards Https and Http2
Has anybody managed to get apns-collapse-id to work ? Please help.
apns-collapse-id is a request header, so you need to use the HTTP service and not the legacy binary interface.

Payload for the Watch App Notification

When creating a new Apple Watch App in Xcode, the following APNS payload example is created:
{
"aps": {
"alert": {
"body": "Test message",
"title": "Optional title"
},
"category": "myCategory"
},
"WatchKit Simulator Actions": [
{
"title": "First Button",
"identifier": "firstButtonAction"
}
],
"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App." }
I am confused by the use of body and title within the alert dictionary. The following payload is ordinarily used in iOS apps:
{
"aps": {
"alert": "Test message",
"title": "Opt title",
"category": "default"
},
"WatchKit Simulator Actions": [
{
"title": "First Button",
"identifier": "firstButtonAction"
}
],
"customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."
}
Which is the correct way? Although a default payload file is created in this way, the Apple documentation provides a screenshot using the latter variant.
In the Local and Remote Notification Programming Guide(Table 3-1), the value type of alert key can be string or dictionary, as Dhawal said, both formats are correct.
If alert is dictionary, it can contain title, body, title-loc-key etc(Table 3-2). What's the purpose of title key? This key was added in iOS 8.2 which contain WatchKit, and WatchKit has a Short-Look Notification interface, there is no enough space for full notification, so Apple Watch use title to describe the purpose of the notification and display in Short-Look Notification.
(source: edgekey.net)
In this picture, "Gray's Birthday" is the title in alert. Because you can't see Short-Look Notification in simulator, you should test result of title key in REAL Apple Watch.
Based on the Apple documentation, here is the following keys that you can have in the aps dictionary: alert, badge, sound and content-available. And here is keys that you can have in the alert dictionary: title, body, title-loc-args etc. You can check it the Push Notification Programming Guide for more information here:
Push Notification Programming Guide
Check this as well for your WatchKit Extension, Apple Watch Programming Guide
Also, the second way you describe should be the correct way. I just create a Watch App and it includes the sample payload. And the category is put inside the aps outside the alert which should be incorrect in this case. Hope this helps. It means that aps can only contain those four default properties. So, category should be contained inside the alert.
Apple Documentation for Push Notification Payload has mentioned a Note,
Note: If you want the device to display the message text as-is in an
alert that has both the Close and View buttons, then specify a string
as the direct value of alert. Don’t specify a dictionary as the value
of alert if the dictionary only has the body property.
So as per above note, both formats are correct but we should use second format (alert with directly text value instead of dictionary) when you only need body property in Alert. If you need to use other child properties of Alert like body, title,...then you should use first format (Alert with dictionary as value).

Apple Watch notification error "Unhandled Category"

I'm working with Apple Watch Notifications.
I defined my own category "myAppcategory" and passed the same to notification payload.
But, still i'm receiving below warning in XCode.
Warning: Notification category "(null)" not found. Define this category or a default category in your storyboard.
And result in watch simulator is below
Can any one help me to fix this issue?
I recently had the same issue. For me it was an invalid push notification payload. If you're using a .apns file (for example the sample one provided for notification controllers) to test make sure it's formatted correctly.
This issue is most probably is caused after a mistaken edit that broke the formatting on the PushNotificationPayload.apns file. To resolve this issue fix the formatting in your .apns file.
The most problematic part is when you add data to alert. Make sure you add comma after each entry, except last one.
"alert": {
"body": "Test message",
"title": "Optional title",
"name": "MyName"
},
Like this.
Try to remove application from iphone simulator. I had the same error and I've resolved it in this way
Also, watch for the comma in the button list
"WatchKit Simulator Actions": [
{
"title": "Stop/Save Activity",
"identifier": "stopsave"
}, // <===== THIS ONE HERE
{
"title": "Cancel Activity",
"identifier": "cancel"
}
],
You should add category value in your payload and define it in your storyboard.
"aps": {
"alert": "Test message",
"sound": "Optional title",
"category":"myCategory"
}
reference: https://developer.apple.com/library/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/CustomzingthePushNotificationInterface.html

Is the following structure with body and loc-args in the same notification payload valid?

Would the following payload notification cause any problems if you have body and loc-args and loc-key in the same payload?
{"aps":{"alert":{"body" : "Bob wants to play poker", "loc-key":"general","loc-args":["Test DEFAULT."]},"badge":1,"sound":"default"}}
The reason I have it set up like this. Is that I am not sure if the notification to just have a body in would be handle during the app, so it would use the localized loc-args in with the following key-valus in the localizable string:
"general"="%#";
I've made several tests.
For example sending
{
"aps": {
"alert": {
"body": "hello world!",
"loc-args": ["andy", "my text"],
"loc-key": "comment"
}
},
"sound": "default",
"extras": {}
}
On my device I have a string in the loc file that looks like:
"comment" = "%# has comment %#";
So the notification that I'm receiving is:
Andy has comment my text
Now I've sent a new notification like this:
{
"aps": {
"alert": {
"body": "hello world!",
"loc-args": ["andy", "my text"],
"loc-key": "does_not_exist"
}
},
"sound": "default",
"extras": {}
}
with a loc-key that does not exist, and the notification that I'm receiving is
does_not_exist
so as kadam said in his answer:
Ultimately, one can infer from the docs that body and loc-key should
be used in an exclusive or relationship. Either one or the other, but
not both.
Blockquote
According to this post, if the key is missing the key is used. That's why 'body' is always ignored and is EITHER ONE or the OTHER.
There are two options to localize an alert message embedded in a push notification. One is to localize on the server and send the localized message as a string value of the alert key. Eg:
{"alert": "A localized sentence."}
Optionally, if you want to also set the launch image and/or have the view button of the alert read something other than 'View', you would replace the string value of alert with a dictionary. In this case the string value should be the value of body in the dictionary. Eg:
{'alert':
{'body: "A localized sentence.", action-loc-key: "...", launch-image: "..."'
}}
The docs warn that if you only have the body key in the dictionary, don't use a dictionary. Instead set the value of body as the string value of alert.
The other option is to use the loc-key, loc-args pair and have the localization take place on the device.
Although I have not verified which one iOS defaults to if both body and loc-key are present in the alert dictionary, I guess it will pick one and ignore the other. Someone with access to a working push notification dev setup could verify which one.
Ultimately, one can infer from the docs that body and loc-key should be used in an exclusive or relationship. Either one or the other, but not both.
I'm not 50 yet so I can't comment but to follow-up on #kadam answer, just for future reference to those who will visit this question.
Although I have not verified which one iOS defaults to if both body and loc-key are present in the alert dictionary, I guess it will pick one and ignore the other. Someone with access to a working push notification dev setup could verify which one.
I have tried it and it defaults to the value of loc-key if it has a value on the app itself. If none then it uses the value of body.

Resources