gpt4 book ai didi

android - 我如何从 cordova 中的服务器获取推送通知?

转载 作者:行者123 更新时间:2023-11-29 11:57:10 25 4
gpt4 key购买 nike

在我的 Cordova 应用程序 iOS/Android 中,我的应用程序将检查是否有互联网连接,它将从服务器(网络服务)接收数据并使用通知

我试过插件“katzer/cordova-plugin-local-notifications”,但它会从我的应用程序发出本地通知,而不是从服务器或网络服务或 url 发出通知

我试过的所有例子都是这样

cordova.plugins.notification.local.schedule({
id: 1,
title: "Production Jour fixe",
text: "Duration 1h",
firstAt: monday_9_am,
every: "week",
sound: "file://sounds/reminder.mp3",
icon: "http://icons.com/?cal_id=1",
data: { meetingId:"123#fg8" }
});

cordova.plugins.notification.local.on("click", function (notification) {
joinMeeting(notification.data.meetingId);
});

我的意思是,当用户在我的应用程序中关注部门时,他将收到有关该部门的所有项目和新消息,并且在新通知中我需要知道我该怎么做?

最佳答案

我使用一个简单的插件从服务器端获取通知到我的 cordova 应用程序。你可以看看这个插件:LINK

好用吗?

JavaScript

var push = PushNotification.init({
android: {
senderID: "12345679"
},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});

push.on('registration', function(data) {
// data.registrationId
});

push.on('notification', function(data) {
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});

push.on('error', function(e) {
// e.message
});

Server (using C#)

public string SendGCMNotification(string apiKey, string deviceId, string postData)
{
string postDataContentType = "application/json";
apiKey = "AIzaSyC13...PhtPvBj1Blihv_J4"; // hardcorded
deviceId = "da5azdfZ0hc:APA91bGM...t8uH"; // hardcorded

string message = "Your text";
string tickerText = "example test GCM";
string contentTitle = "content title GCM";
postData =
"{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
"\"data\": {\"tickerText\":\"" + tickerText + "\", " +
"\"contentTitle\":\"" + contentTitle + "\", " +
"\"message\": \"" + message + "\"}}";


ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

//
// MESSAGE CONTENT
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

//
// CREATE REQUEST
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
Request.Method = "POST";
Request.KeepAlive = false;
Request.ContentType = postDataContentType;
Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.ContentLength = byteArray.Length;

Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

//
// SEND MESSAGE
try
{
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
var text = "Unauthorized - need new token";
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
var text = "Response from web service isn't OK";
}

StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadToEnd();
Reader.Close();

return responseLine;
}
catch (Exception e)
{
}
return "error";
}

public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
}

关于android - 我如何从 cordova 中的服务器获取推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38562604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com