How to set title for iOS push notification using pushd? - ios

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
}
}

Related

Firebase not displaying rich push notification

I am using firebase notification in my application. I want to send rich notification but firebase console don't have UI for same. So I am trying to send the payload using postman.
I have created Notification Service Extension for handling content but it never gets called.
Deployment target in xCode is iOS 10.0 and iPad has iOS 11
Json payload(Not Working):
{
"to":"/topics/dev_news",
"mutable_content": true,
"data":
{
"attachment-url":"my image url",
"media_type":"image"
}
}
FYI:
I am able to receive push if I add "notification" key-value in payload.
Json payload(Working but without media):
{
"to":"/topics/dev_news",
"mutable_content": true,
"data":
{
"attachment-url":"my image url",
"media_type":"image"
},
"notification" : {
"title" : "demo push",
"body" : "this is push body"
}
}
Any idea about this??
Answering to my own question, I was searching for attachment-url in data dictionary (which I was passing in the payload) in service extension class. But attachment-url comes directly in the content.userInfo (It is not wrapped inside the data dictionary) in extension class.
Wondering how I can make such a silly mistake..!! :D

Multi line title in push notification for 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.

Is it possible to hide some part of message in iOS Notification?

I am sending "Push Notification" from Amazon SNS to iOS devices. While sending a notification, i want to hide the url and to show remaining string in notification to the user.
For Instance :
Notification is like this "Hi customers
https://www.google.co.in/?gfe_rd=cr&ei=zCyeVbuiMZK_-APxlYHoBA welcome"
Show the notification to the user as "Hi customers welcome"
And i want to store the entire notification message in database.
If user clicks on notification then i have to open that link in embedded browser. But that url will not be visible to the user
Is it possible to hide some part of message in notification ? Advance thanks for any help.
From Apple Push Notification Service:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean. You should not include customer information (or any sensitive data) as custom payload data. [...]
An example is provided in the documentation:
{
"aps" : { "alert" : "Message received from Bob" },
"acme2" : [ "bang", "whiz" ]
}
In your case I suggest a payload like this:
{
"aps" : { "alert" : "Welcome Customers!" },
"welcome-url" : "https://www.google.co.in/?gfe_rd=cr&ei=zCyeVbuiMZK_-APxlYHoBA"
}

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).

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