简体   繁体   中英

Phonegap Android push notification using Adobe Plugin

I am using Adobe phonegap plugin for push notification and developing android application.

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

The server to send push notification is 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.

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

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. But on Android you have to explicitly code for it.

One option is to use the Cordova StatusBarNotification plugin along with this one. If you want a faster solution, simply add the native Java code into your GCMIntentService.java onMessage() function, as in the following code:

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. For instance, in the sample project it is called MainActivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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