2013-02-25 22 views
0

我正在使用Adobe phonegap插件推送通知並開發android應用程序。使用Adobe Plugin的Phonegap Android推送通知

http://www.adobe.com/devnet/phonegap/articles/android-push-notifications-with-phonegap.html

服務器發送推送通知是PHP作爲

<?php 
// Replace with real BROWSER API key from Google APIs 
$apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
// Replace with real client registration IDs 
$registrationIDs = array("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); 
// Message to be sent 
$message = "there is an update, waiting for you to install..."; 
// Set POST variables 
$url = 'https://android.googleapis.com/gcm/send'; 

$fields = array(
      'registration_ids' => $registrationIDs, 
      'data'    => array("message" => $message), 
      ); 

$headers = array( 
       'Authorization: key=' . $apiKey, 
       'Content-Type: application/json' 
      ); 

// Open connection 
$ch = curl_init(); 

// Set the url, number of POST vars, POST data 
curl_setopt($ch, CURLOPT_URL, $url); 

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

// Execute post 
$result = curl_exec($ch); 

// Close connection 
curl_close($ch); 

echo $result; 

編譯Android的代碼我收到註冊成功與REGID後。

發送通知後的反應是成功的

{ 
multicast_id: 5022434288122138000, 
success: 1, 
failure: 0, 
canonical_ids: 0, 
results: [ 
    { 
    message_id: "0:1361774232591520%8eab850df9fd7ecd" 
    } 
] 
} 

但仍我無法得到我的電話通知。

不知道我在做什麼錯。

編輯:

對不起,我聽明白了推送通知消息,但並沒有顯示它。現在
http://www.adobe.com/devnet/phonegap/articles/android-push-notifications-with-phonegap.html#articlecontentAdobe_numberedheader_4

對我的作品的期待!!:

+0

對不起,我得到推送通知消息,但沒有顯示它。 – Pawan 2013-02-25 09:42:13

+0

我無法發送通知到設備bcoz我寫的代碼就像你寫的代碼一樣,然後我上傳這個頁面到我的服務器,運行後我遇到了像「未經授權的錯誤401」的錯誤,請你幫幫我。這我的網址http://stackoverflow.com/questions/23677313/android-phonegap-send-push-notification-using-google-cloud-messaging-using-php – 2014-05-15 11:55:22

回答

0

我找到了答案在這裏

狀態欄通知

由於插件只是收到你用它做什麼消息,是否您的應用程序正在運行或不但是從那裏什麼都不做,你必須選擇。一個常見的要求是在本地狀態欄中顯示一條消息。在iOS上,該過程是不同的,通知會自動顯示。但在Android上,您必須明確地編寫代碼。

一個選項是使用Cordova StatusBarNotification插件和這一個。如果你想有一個更快的解決方案,只需添加本地Java代碼到你GCMIntentService.java的onMessage()函數,如下面的代碼:

String message = extras.getString("message"); 
String title = extras.getString("title"); 
Notification notif = new Notification(android.R.drawable.btn_star_big_on, message, System.currentTimeMillis()); 
notif.flags = Notification.FLAG_AUTO_CANCEL; 
notif.defaults |= Notification.DEFAULT_SOUND; 
notif.defaults |= Notification.DEFAULT_VIBRATE; 

Intent notificationIntent = new Intent(context, TestSampleApp.class); 
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

notif.setLatestEventInfo(context, title, message, contentIntent); 
String ns = Context.NOTIFICATION_SERVICE; 
NotificationManager mNotificationManager = (NotificationManager) 
context.getSystemService(ns); 
mNotificationManager.notify(1, notif); 

此外,在該文件的頂部添加以下的進口支持上面的代碼:

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 

請務必將YourActivityClassName.class替換爲擴展DroidGap的存根類的名稱。例如,在示例項目中,它被稱爲MainActivity。

+0

你好,我試着用你的上述方法。我不知道爲什麼。但是我得到了「401」的錯誤。我必須提供使用類型的權限來運行此? – user3201500 2014-09-17 10:09:50