2016-07-25 35 views
1

在我科爾多瓦應用的iOS/Android的我的應用程序將檢查是否有互聯網連接,將接收來自服務器(Web服務)的數據,並使用通知如何從服務器獲得推送通知?

我試過插件「katzer /科爾多瓦 - 插件本地的通知」,但它將使通知當地從我的應用程序不能從服務器或Web服務或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); 
}); 

我的意思是,當用戶後續部門在我的應用程序,他將收到的所有項目和新關於這個部門和新近通知我需要知道我怎麼能做到這一點?

+0

,想到的第一件事是必須有一千教程/例子,可以可以通過簡單的Google搜索cordova推送通知找到。 – Roope

+1

@Roope所有的一千個教程/例子,我發現谷歌是本地的應用程序而不是從服務器 – user5740661

回答

0

我用一個簡單的插件從服務器端獲取通知給我的科爾多瓦應用程序。你可以看看這個插件: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 
}); 

服務器(使用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; 
}