I am trying to send push notification from the admin device, to all devices (segment:"ALL"). I can't seem to get it to work with this code.
let data = [
"contents": ["en": "\(message)"],
"included_segments": ["All"],
"excluded_segments": ["None"],
"ios_badgeType": "Increase",
"ios_badgeCount": 1
]
OneSignal.defaultClient().postNotification(data)
The "None" segment exists and has 0 users in it.
"All" has all active users.
When I post the notification I get an error
ERROR: Create notification failed
while this does work without any problems:
let data = [
"contents": ["en": "\(message)"],
"include_player_ids":["20a32b7a-4254-4e03-a24c-13fe8bd91fff"],
"ios_badgeType": "Increase",
"ios_badgeCount": 1
]
OneSignal.defaultClient().postNotification(data)
You can only use include_player_ids from postNotification as other targeting parameters like included_segments require your OneSignal App REST API key.
If you need to target all users you should first make a call to your server from your app where you can safely use the create notification REST API POST call.
Related
When we are sending a FCM data message with the Firebase Admin SDK to an iOS device we are getting the following exception.
com.google.firebase.messaging.FirebaseMessagingException: Request contains an invalid argument.
We are using com.google.firebase:firebase-admin:6.7.0 and the message is build like this.
Message.builder()
.setApnsConfig(ApnsConfig.builder()
.putHeader("apns-priority", "10")
.setAps(Aps.builder()
.setContentAvailable(true)
.build())
.build())
.putAllData(data)
.setToken(token)
.build();
I don't think that there is something wrong with the token itself, because sending a push notification to this token works.
Also the message size shouldn't be an issue, because it is basically just an id in it.
I'm not sure if it happens on all iOS devices.
Am i missing something on building the data message?
Update
I checked the details included at the exception and there it looks like something is wrong with the token.
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
},
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.token",
"description": "Invalid registration token"
}
]
}
]
}
}
But when i send a data message to the same token using postman, there is no error and the response looks like this.
{
"multicast_id": 7455611938954436367,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1614145427166557%c5757de0f9fd7ecd"
}
]
}
I think i figured it out.
My problem was that i was setting the apns-priority to "10". According to the documentation, notifications with a priority of "10" have to be delivered immediatly.
If i put my device into flight mode for example, and sending my data push it failes with the mentioned firebase exception about an invalid token. Which is very missleading in my opinion.
If i set the apns-priority to "5" everything works fine.
It may happen if you try to use a push notification with only data (silent push notification) which do not required the user to have granted permission to receive on iOS, but you set some iOS specific parameters in the request.
In this case, the error does not occur if the user has granted permission to receive push notifications or you don't specify any iOS specific parameter.
I'm working with firebase to add push notification feature to an app, everything is fine with the push notification it self but in some state of the app the push notification is not receive.
Foreground / Background:
{
"data":{
"title": "title",
"message": "message"
},
"content_available":true,
"priority":"high",
"registration_ids":[
""
]
}
The problem comes when the app is killed by the user, I send the same http request but the app didn't receive the remote notification.
I was testing with the console on firebase and when the app is killed the notification it's receive but I couldn't replicate the console request to http request
this is the content of notification when I made it through firebase console
[
"google.c.a.c_l": label optional,
"google.c.a.e": 1,
"google.c.a.ts": IntValue,
"google.c.a.udt": 0,
"gcm.n.e": 1,
"aps": {
alert = {
body = mensaje;
title = title;
};
},
"google.c.a.c_id": IntValue,
"gcm.message_id": 0:1502103735577088%9218dc2f9218dc2f
]
This is how iOS works, basically. When your app is killed by the user, it no longer receives background notifications.
Here's a somewhat-old-but-still-accurate link with some more detailed information: http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/
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.
In my app I've got buttons where if the user presses it they are then 'subscribed' to that channel and will receive notifications for that and which ever other ones they subscribe to. I wanted to know how to do this with OneSignal you can create user segments and send notifications to only those specific groups. So if I create a segment in OneSignal how can I register users to that segment and remove them?
Xcode, swift 3, iOS 10
Use Tags. (from FrontEnd or API)
Example using Frontend:
window.plugins.OneSignal.sendTags({key: "value", key2: "value2"});
Example using API:
URL:
https://onesignal.com/api/v1/players/PLAYER_ID
METHOD:
PUT
HEADERS:
ContentType: application/json
BODY (json):
{
"app_id": "APP_ID",
"tags": {
"key1": "value1",
"key2": "value2",
}
}
Im using the new Firebase platform. Am trying to get a push notification sent by my app server and delivered to my iPhone.
I have the setup working where I manually send the message with the Firebase notifications area on the website, but when I try and send the message with a POST to https://fcm.googleapis.com/fcm/send I get no message being delivered to the device.
I'm sending the following (with auth headers)
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to" : "<registration token>"
}
I'm getting a 200 response from the POST with the following body:
{
"multicast_id": 5511974093763495964,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1463685441784359%3ad254b53ad254b5"
}
]
}
If I try and send to this device directly through the Firebase website it works, but the above form post doesn't. No idea where to go from here!
On iOS the priority field seems mandatory.
{
"to": "cHPpZ_s14EA:APA91bG56znW...",
"priority": "high",
"notification" : {
"body" : "hello!",
"title": "afruz",
"sound": "default"
}
}
If the API returns you a message_id it means that your message has been correctly accepted and it will eventually be delivered to the device.
On Android, messages are delivered as soon as possible (provided that the device is connected of course).
On Apple devices, IF the application is closed or in background, the notification is sent through Apple infrastructure and can be delayed accordingly with Apple documentation.
To reduce the delay of the prioritymessages sent to Apple device you can use the priority parameter.
More details: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
I found the message field to be required, as well as the priority field to deliver a message with a POST.
message is not required (and labeled as optional) in the Firebase Console.
I resolved adding the notification tag in request body without priority.
Here is a recap: