簡體   English   中英

Google雲消息傳遞GCM-推送通知未發送(服務器端)

[英]Google cloud messaging GCM - Push Notification not being sent (Server Side)

我能夠獲取設備ID並將其保存到我的數據庫中,並且在發生某些情況時,我嘗試發送推送通知,但未將其發送到手機。 這是我在PHP中所做的事情:

$url = 'https://android.googleapis.com/gcm/send';

$device_ids = array( $device_id );

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

$t_data = array();
$t_data['message'] = 'Someone commented on your business.';

$t_json = array( 'registration_ids' => $device_ids , 'data' => $t_data );

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: key=my_id', 'Content-Type: application/json' ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $t_json ) );

curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec($ch);
if ($result === FALSE)
{
     die('Curl failed: ' . curl_error($ch));
}

curl_close($ch);

這是我從curl_exec調用中得到的結果:

{"multicast_id":8714083978034301091,"success":1,"failure":0,"canonical_ids":0,"r‌​esults":[{"message_id":"0:1350807053347963%9aab4bd8f9fd7ecd"}]} 

我想知道的一件事是,是否需要在應用程序中做一些額外的事情,例如編寫自己的Reciever類? 謝謝!

編輯:

這是我的GCMIntentService類:

package com.problemio;

import static com.google.android.gcm.GCMConstants.ERROR_SERVICE_NOT_AVAILABLE;
import static com.google.android.gcm.GCMConstants.EXTRA_ERROR;
import static com.google.android.gcm.GCMConstants.EXTRA_REGISTRATION_ID;
import static com.google.android.gcm.GCMConstants.EXTRA_SPECIAL_MESSAGE;
import static com.google.android.gcm.GCMConstants.EXTRA_TOTAL_DELETED;
import static com.google.android.gcm.GCMConstants.EXTRA_UNREGISTERED;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_MESSAGE;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK;
import static com.google.android.gcm.GCMConstants.VALUE_DELETED_MESSAGES;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.google.android.gcm.GCMBaseIntentService;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import utils.GCMConstants;

public class GCMIntentService extends GCMBaseIntentService 
{
    public GCMIntentService() 
    {
            super(ProblemioActivity.SENDER_ID);
    }

    @Override
      protected void onRegistered(Context ctxt, String regId) {
        Log.d(getClass().getSimpleName(), "onRegistered: " + regId);
        Toast.makeText(this, regId, Toast.LENGTH_LONG).show();
      }

      @Override
      protected void onUnregistered(Context ctxt, String regId) {
        Log.d(getClass().getSimpleName(), "onUnregistered: " + regId);
      }

      @Override
      protected void onMessage(Context ctxt, Intent message) {
        Bundle extras=message.getExtras();

        for (String key : extras.keySet()) {
          Log.d(getClass().getSimpleName(),
                String.format("onMessage: %s=%s", key,
                              extras.getString(key)));
        }
      }

      @Override
      protected void onError(Context ctxt, String errorMsg) {
        Log.d(getClass().getSimpleName(), "onError: " + errorMsg);
      }

      @Override
      protected boolean onRecoverableError(Context ctxt, String errorMsg) {
        Log.d(getClass().getSimpleName(), "onRecoverableError: " + errorMsg);

        return(true);
      } 
}

更新:

查看LogCat,結果表明消息正在到達設備。 但是由於某種原因,設備沒有顯示推送通知。

從響應看來,消息已傳遞。 在Android上,您應該具有擴展GCMBaseIntentService的GCMIntentService類,以在設備上接收消息。 您應檢查SDK示例中隨附的gcm-demo-client,以獲取有關如何在應用程序上實施此方法的好方法。 在那里,您只需要在CommonUtilities類中設置SENDER_ID(您的Google proyect號)即可接收來自服務器的消息。

更多信息在這里

要在GCMIntentService上生成通知,您可以使用:

 //Issues a notification to inform the user that server has sent a message.

private static void generateNotification(Context context, String message, String title,) {

        int icon = R.drawable.logo;

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

        Intent notificationIntent = new Intent(context, AnActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);        
        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);            

         Notification notification = new NotificationCompat.Builder(context)
         .setContentTitle(title)
         .setContentText(message)
         .setContentIntent(intent)
         .setSmallIcon(icon)
         .setLights(Color.YELLOW, 1, 2)
         .setAutoCancel(true)
         .setSound(defaultSound)
         .build();

        notificationManager.notify(0, notification);
}

您是否還在清單上注冊了接收者? 在應用標簽下?

    <!--
      BroadcastReceiver that will receive intents from GCM
      services and handle them to the custom IntentService.

      The com.google.android.c2dm.permission.SEND permission is necessary
      so only GCM services can send data messages for the app.
    -->
    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.google.android.gcm.demo.app" />
        </intent-filter>
    </receiver>

    <!--
      Application-specific subclass of GCMBaseIntentService that will
      handle received messages.

      By default, it must be named .GCMIntentService, unless the
      application uses a custom BroadcastReceiver that redefines its name.
    -->
    <service android:name=".GCMIntentService" />

僅當您計划使您的消息覆蓋該類型的先前消息時,才需要塌陷關鍵字。 因此,如果您發送的消息要求應用程序進行同步,則可以給它提供一個折疊鍵,這樣它只會發送1條同步消息。 官方文檔描述了如何使用它。

從GCM服務器發送通知時,要使用哪個網址? https://android.googleapis.com/gcm/sendhttps://gcm-http.googleapis.com/gcm/send

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM