简体   繁体   English

使用Adobe Plugin的Phonegap Android推送通知

[英]Phonegap Android push notification using Adobe Plugin

I am using Adobe phonegap plugin for push notification and developing android application. 我正在使用Adobe phonegap插件进行推送通知并开发android应用程序。

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

The server to send push notification is PHP as 发送推送通知的服务器是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;

After compilation the android code i am getting registration success with regid. 编译完android代码后,我使用regid获得注册成功。

after sending the notification the response is success as 发送通知后,响应为成功

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

but still i am unable to get notification on my phone. 但我仍然无法在手机上收到通知。

Not sure whats wrong i am doing. 不知道我在做什么错。

Edited: 编辑:

Sorry i was getting the push notification message but was not displaying it. 抱歉,我收到了推送通知消息,但没有显示。

I found the answer here: 我在这里找到了答案:
http://www.adobe.com/devnet/phonegap/articles/android-push-notifications-with-phonegap.html#articlecontentAdobe_numberedheader_4 http://www.adobe.com/devnet/phonegap/articles/android-push-notifications-with-phonegap.html#articlecontentAdobe_numberedheader_4

Works for me now as expected!! 现在按预期为我工作!!

Status bar notification 状态栏通知

Since the plugin simply receives the message—whether your app is running or not—but doesn't do anything with it from there, you have choices for what you do with it. 由于该插件仅接收消息(无论您的应用是否正在运行),但从那里不对其进行任何操作,因此您可以选择使用该消息的方式。 One common requirement is to show a message in the native status bar. 一个常见的要求是在本机状态栏中显示一条消息。 On iOS, the process is different and the notification is automatically shown. 在iOS上,过程有所不同,并且通知会自动显示。 But on Android you have to explicitly code for it. 但是在Android上,您必须为其明确编写代码。

One option is to use the Cordova StatusBarNotification plugin along with this one. 一种选择是与此一起使用Cordova StatusBarNotification插件。 If you want a faster solution, simply add the native Java code into your GCMIntentService.java onMessage() function, as in the following code: 如果需要更快的解决方案,只需将本机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);

Also, add the following imports at the top of the file to support the above code: 另外,在文件顶部添加以下导入以支持上述代码:

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

Be sure to replace YourActivityClassName.class with the name of your stub class that extends DroidGap. 确保用扩展DroidGap的存根类的名称替换YourActivityClassName.class。 For instance, in the sample project it is called MainActivity. 例如,在示例项目中,它称为MainActivity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM