i try to play a sound for remote notifications. the app is registered for remoteNotificatiosTypeSound, the sound is enable in notifications phone settings.
the structure of remote notification is like that in console:
{
aps = {
alert = "Notification received";
badge = 1;
payload = {
action = A;
date = "2014-09-16";
idmessage = 243722;
message = "";
time = "11:39:33";
title = "";
};
sound = alarm4.wav;
};
}
It is an error somewhere?
Thank you
sound = alarm4.wav
Does the device have this sound file?
Try
"sound": "default"
Also, this should be a JSON string so you should have : instead of =
Related
I know this question have been ask a lot of time in android but not ios. So I already test out push notification with FCM it work fine. Mine problem is wether it's a way to create custom notification rather than letting the system to handle the notification with the notification payload?
-Users can send data messages with FCM.What you have to send is,
Sample PostMan Json data:
{ "data": {
"any key": "any value",
"any key 2": "any value 2"
},
"to" : "token received to mobile from FCM"
}
Payload inside "data" can change according to the requirement.
Mobile end:
application(_:didReceiveRemoteNotification:) override method will get fire.So it is unto developer to extract data from the userInfo property in iOS and use it.
For ios using c# working code:-
public bool NotificationToiosDevice()
{
bool str;
try
{
//Create the web request with fire base API
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
//serverKey - Key from Firebase cloud messaging server
tRequest.Headers.Add(string.Format("Authorization: key={0}", "AAAALNl-P7o:APA91bE38khU73UTdIj7L6LvVS3uI6f47REmxl.................................."));
//Sender Id - From firebase project setting
tRequest.Headers.Add(string.Format("Sender: id={0}", "12345"));
tRequest.ContentType = "application/json";
var payload = new
{
to = "dDDbFltwE5o:APA91bFmZZ-c1pNp................",
priority = "high",
content_available = true,
notification = new
{
title = "Title Of Notification",
},
data = new
{
body = new { Parameter1 = "FirstParameterValue", Parameter2 = "SecondParameterValue" },
},
};
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
// str = sResponseFromServer;
}
}
}
}
str = true;
}
catch (Exception ex)
{
str = false;
}
return str;
}
From CloudKit I receive example response:
[AnyHashable("aps"): {
"content-available" = 1;
}, AnyHashable("ck"): {
ce = 2;
cid = "iCloud.pl.blueworld.fieldservice";
nid = "bb501155-f914-4f8b-b58e-4c24921727f5";
qry = {
dbs = 2;
fo = 3;
rid = "27C8C222-11B3-4831-A125-EAFEF8DB6ADD";
sid = "E32D6B20-3F81-464E-ACA8-2DD29FA93CF3";
zid = "_defaultZone";
zoid = "_defaultOwner";
};
}]
At any time I can fetch missed notifications and mark them as read to prevent from receiving it again.
Simply I do it like this:
OperationQueue().addOperation(CKMarkNotificationsReadOperation(notificationIDsToMarkRead: [queryNotification.notificationID!]))
nid field links to notification connected with this response.
After I receive CloudKit response I need to mark this notification as read. All I have is String, but operation to mark it as read expects CKNotificationID object. How can I create such object?
I want to create ingame pause, so I register local notification to notify user when it's ended.
But there are different purposes for local notification that I use.
So I used userInfo to mark a notification that responsible for pause.
private void ScheduleNotification(DateTime date, string message)
{
#if UNITY_IOS
LocalNotification not = new LocalNotification();
not.applicationIconBadgeNumber = NotificationServices.localNotificationCount + 1;
not.alertBody = message;
not.fireDate = date;
not.userInfo["pause"] = 0;
NotificationServices.ScheduleLocalNotification(not);
#endif
}
But I don't know how to get LocalNotification that was activated, how to do it?
If I send notification to iOS device from FCM console I am getting the message alert on notification center (swipe down from the Top of the screen to view Notification Centre).
didRecievedRemoteNotification Output is :
{
aps = {
alert = "From console";
};
"gcm.message_id" = "0:1470206236110595%b2c76869b2c76869";
"gcm.n.e" = 1;
"google.c.a.c_id" = 2979094970349938289;
"google.c.a.e" = 1;
"google.c.a.ts" = 1470206236;
"google.c.a.udt" = 0;
}
But if send notification to iOS device I am sending using api from my server I am not receiving message alert on notification center (swipe down from the Top of the screen to view Notification Centre).
didRecievedRemoteNotification Output is :
{
"collapse_key" = "do_not_collapse";
from = 67981113117;
message = "alert from api";
time = "03-08-2016 12:44:53";
}
My API code is which is in C#:
string RegIDs = "some id";
var appID = "some id";
var SenderID = "some id";
var value = Text1.Text;
WebRequest tRequest;
tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "POST";
tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", appID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SenderID));
//Data_Post Format
// string postData = "{'collapse_key' : 'demo', 'registration_id': [ '" + regId + "' ],
//'data': {'message': '" + Label1.Text + "'},'time_to_live' : '3' }";
//json for android
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
+ value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + RegIDs + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = (WebResponse)tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
Label3.Text = sResponseFromServer; //printing response from GCM server.
tReader.Close();
dataStream.Close();
tResponse.Close();
Another alternative in order to test push notifications without using Firebase console is using Postman. Attached two images with configuration details.
Image 1: Set notification payload
Image 2: Set request headers ( Content-Type and Authorization )
Firebase push notification endpoint: https://fcm.googleapis.com/fcm/send
I get the tokenUpdate from the device, and then I try to send a push notification. But I don't think that it is working as when I send a push notification, I do not get any response from the device. Isn't it suppose to poll the server to check for any MDM Commands? Instead I keep receiving token update.
String cToken = token;
String cAlert = message;
// Ready to create the push notification
byte[] buf = new byte[256];
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
char[] tokenChars = token.ToCharArray();
byte[] deviceTokenBytes = new byte[tokenChars.Length];
for (int i=0; i < deviceTokenBytes.Length; i++)
{
deviceTokenBytes[i] = Convert.ToByte(tokenChars[i]);
}
// byte[] deviceToken = HexToData(cToken);
bw.Write(deviceTokenBytes);
// Create the APNS payload - new.caf is an audio file saved in the application bundle on the device
//string msg = "{\"aps\":{\"alert\":\"" + cAlert + "\",\"badge\":" + iBadge.ToString() + ",\"sound\":\"new.caf\"}}";
string msg = "{\"mdm\":\"+ mPushMagic +"\"}";
// Write the data out to the stream
// bw.Write((byte)msg.Length);
bw.Write(msg.ToCharArray());
bw.Flush();
if (sslStream != null)
{
sslStream.Write(ms.ToArray());
return true;
}
return false;
}
UPDATED: I removed the '<' from the pushmagic id
I'd recommend removing the chevrons from your mdm message to start with.