【问题标题】:Not Receiving notification from GCM sent from Notification Server未收到来自 Notification Server 的 GCM 通知
【发布时间】:2016-09-14 10:26:59
【问题描述】:

我正在尝试为 Android 应用程序实现通知服务器。我遵循了教程http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/ 并且工作正常。但是现在服务器端的整个事情都由我的通知服务器处理,我在用户登录时与它共享我的设备 ID 和用户 ID。我的通知服务器成功地将 regid 发送到 gcm,但我没有在我的设备上收到通知。我的 onReceive() 现在没有被调用。这是我的服务器从 gcm 收到的响应。 messageId=0:1463550332634647%fd07​​91fdf9fd7ecd

Config.java

public interface Config {

//static final String APP_SERVER_URL = "http://192.168.100.27:8081/GCMServer/GCMNotification?shareRegId=1";//earlier it was used to share regid with server
static final String GOOGLE_PROJECT_ID = "299876636766";
static final String MESSAGE_KEY = "message";

}

GCMNotificationIntentService.java

 public class GCMNotificationIntentService extends IntentService {

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GCMNotificationIntentService() {
    super("GcmIntentService");
}

public static final String TAG = "GCMNotificationIntentService";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);
    System.out.println("MessageType is"+messageType);
    String msg = intent.getStringExtra("message");
    System.out.println("mESSAGE IS---->>>"+msg);

    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i < 3; i++) {
                Log.i(TAG,
                        "Working... " + (i + 1) + "/5 @ "
                                + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }

            }
            Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

            sendNotification(""+msg);
            Log.i(TAG, "Received: " + extras.toString());
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    Log.d(TAG, "Preparing to send notification...: " + msg);
    System.out.println("Message is"+msg);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent=null ;
    if(msg.equalsIgnoreCase("call")){
        System.out.println("Inside iffffffffffffffffffffff");
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
        wl.acquire();
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
        //setResultCode(Activity.RESULT_OK)

        wl.release();
        contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, SplashScreen.class), 0);
        System.out.println("the content intent is"+contentIntent.toString());

    }else{
        System.out.println("Inside else");
        contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);


    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    Log.d(TAG, "Notification sent successfully.");
}
}

GCMBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(),
            GCMNotificationIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
 }

【问题讨论】:

  • 您收到任何错误日志吗?你是否也更改了 php 文件中的密钥?
  • GCM 服务器的响应是什么?
  • @NiranjPatel 一个 messageId(一个长字符串)和规范注册 id
  • @SwagDevelopers 你可以用 GCM 服务器的完整响应来编辑你的问题
  • @NiranjPatel 完成。

标签: java android google-cloud-messaging android-notifications notificationservices


【解决方案1】:

如果您确实收到了示例代码中的消息;我认为您的项目 gcm 中不允许您的服务器的 IP 地址。或者您的服务器的防火墙阻止 gcm 的消息,如https://stackoverflow.com/a/13470029/2917564。无论哪种方式,我都建议您在服务器上运行一个独立的简单 php 脚本,其中只有您的注册 ID,以确保它不在代码中,您可以找到一个 https://stackoverflow.com/a/11253231/2917564

【讨论】:

    【解决方案2】:

    如果您从 GCM 服务器收到如下成功消息,那么您的设备必须收到通知

    来自 GCM 服务器的成功消息

    {
        "multicast_id": 661164****4469281,
        "success": 1,
        "failure": 0,
        "canonical_ids": 1,
        "results": [{
            "registration_id": "APA91bFWKKC4Bh_B******-H",
            "message_id": "0:14635506***b6f9fd7ecd"
        }]
    }
    

    以下可能是设备未收到消息的情况。

    1. android 端的发件人 ID 错误。
    2. 服务器端 (PHP) 的 Google API 密钥错误。
    3. 您发送到 GCM 服务器的设备注册 ID 错误。

    请一次验证所有值并检查。 如果一切都很完美,那么您必须在设备中收到消息。 请检查您设备中的互联网连接。

    请使用以下内容验证您的Menifest.xml 文件。

    <receiver
                android:name=".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="your_package" />
                </intent-filter>
            </receiver>
    
            <service android:name=".GCMIntentService" />
        </application>
    
        <!-- GCM connects to Internet Services. -->
        <uses-permission android:name="android.permission.INTERNET" />
    
        <!-- GCM requires a Google account. -->
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    
        <!-- Keeps the processor from sleeping when a message is received. -->
        <uses-permission android:name="android.permission.WAKE_LOCK" />
    
        <!-- Creates a custom permission so only this app can receive its messages. -->
        <permission
            android:name="your_package.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
    
        <uses-permission android:name="your_package.permission.C2D_MESSAGE" />
    
        <!-- This app has permission to register and receive data message. -->
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    
        <!-- Network State Permissions to detect Internet status -->
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

    • 我正在发送从 regId=gcm.register() 消息接收的正确 regId。 Api key 也是正确的。检查了两次。
    • 我看到这个帖子stackoverflow.com/questions/21515141/…好像也有类似的问题,但是解决方法和原因我不明白。
    猜你喜欢
    • 2015-07-17
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多