[英]Phonegap Android push notification using Adobe Plugin
我正在使用Adobe phonegap插件進行推送通知並開發android應用程序。
http://www.adobe.com/devnet/phonegap/articles/android-push-notifications-with-phonegap.html
發送推送通知的服務器是PHP as
<?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"
}
]
}
但我仍然無法在手機上收到通知。
不知道我在做什么錯。
編輯:
抱歉,我收到了推送通知消息,但沒有顯示。
現在按預期為我工作!!
狀態欄通知
由於該插件僅接收消息(無論您的應用是否正在運行),但從那里不對其進行任何操作,因此您可以選擇使用該消息的方式。 一個常見的要求是在本機狀態欄中顯示一條消息。 在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;
確保用擴展DroidGap的存根類的名稱替換YourActivityClassName.class。 例如,在示例項目中,它稱為MainActivity。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.