[英]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.