在android中推送通知多次

时间:2016-03-23 08:37:40

标签: php android push-notification google-cloud-messaging

我使用Eclipse创建了一个移动应用程序。

服务器通过GCM(在Php中)发送推送通知。

首次安装APK时,它会发送一个推送通知,该通知正常。对于秒时间(同一设备上的相同APP),它发送两次,第三次,三次,依此类推。

我发现问题是由添加更多相同设备的ID引起的。因此,如果我手动删除所有ID并再次安装APK,它将正常工作。

{
  "_id" : 0,  // auto increment primary key for "Object" collection
  club" : 0,  // auto increment primary key for "Club" collection
}

Android方

$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => $registatoin_ids,'data' => $message,); $headers = array( 'Authorization: key=' . 'asbjadbdb','Content-Type: application/json');        
$ch = curl_init();
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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {  die('Curl failed: ' . curl_error($ch));}
curl_close($ch);
echo $result;

1 个答案:

答案 0 :(得分:1)

这就是我设法使用PHP通过GCM处理推送通知的方法。

服务器是一个复杂的REST服务器,但是现在你只需要理解它的一小部分。

接收并存储Push token

您需要Push token才能确定GCM应向哪个设备发送推送。因此,您需要存储它,但请记住它可能会发生变化,如果发生这种情况,您的应用程序需要将新的服务器发送到服务器,并且需要在数据库中进行更改。

发送Push notification

要发送推送通知,我从数据库中恢复Push token,然后使用PushSender类实际发送push notification

使用push token服务器中的查询检索MySql

使用PushSender类:

$push = new PushSender('The push title','The message',$push_token]);
$ret = $push->sendToAndroid();

// Check $ret value for errors or success

PushSender类:

Class PushSender {

    private $title;
    private $message;
    private $pushtoken;

    private static $ANDROID_URL = 'https://android.googleapis.com/gcm/send';
    private static $ANDROID_API_KEY = 'YOUR-API-KEY';

    public function __construct($title, $message, $pushtoken){

        $this->title = $title;
        $this->message = $message;
        $this->pushtoken = $pushtoken;
    }

    public function sendToAndroid(){

        $fields = array(
            'registration_ids' => array($this->pushtoken),
            'data' => array( "title"=>$this->title, "message" => $this->message ),
        );

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

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, self::$ANDROID_URL);
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER , false );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST , false );

        curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch);

        if( curl_errno($ch) ){
            curl_close($ch);
            return json_encode(array("status"=>"ko","payload"=>"Error: ".curl_error($ch)));
        }

        curl_close($ch);
        return $result;
    }
}

通过服务在Android中接收push notification

public class PushListenerService extends GcmListenerService {
    @Override
    public void onMessageReceived(String from, Bundle data) {

        String message = data.getString("message");

        // Do whatever you need to do
        // Then send the notification
        sendNotification(message);
}

private void sendNotification(String message) {

    Intent intent = new Intent(this, YourClass.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.an_icon)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(Helpers.getString(R.string.push_notification_message))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    try{
        notificationBuilder
                .setLargeIcon(Bitmap.createBitmap(((BitmapDrawable)getDrawable(R.drawable.your_icon)).getBitmap()));

    }catch (NullPointerException e) {
        e.printStackTrace();
    }

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
相关问题