简体   繁体   English

Google Cloud Messaging,正在接收消息,但不生成通知

[英]Google Cloud Messaging, receiving message, but not building notification

I have implemented the Google Cloud Messaging for Android client on my app. 我已经在我的应用中实现了Android版Google Cloud Messaging客户端。 When sent from our 3rd party server, we received a success message: 从我们的第三方服务器发送时,我们收到一条成功消息:

EDITED EDITED

So turns out I am actually receiving the message from the Google Cloud Messaging server, but I am not getting a notification on the phone. 因此,事实证明我实际上是从Google Cloud Messaging服务器收到消息,但没有在电话上收到通知。 I know I am receiving the message because it shows it my Logcat. 我知道我收到消息是因为它显示了我的Logcat。 The only problem is that my notification is not getting built/sent. 唯一的问题是我的通知没有建立/发送。

Why is this happening? 为什么会这样呢? Please note my GcmIntentService is a separate class on its own. 请注意,我的GcmIntentService是一个单独的类。 As well as the BroadcastReceiver . 以及BroadcastReceiver I am guessing it has to do with my sendNotification() method? 我猜想这与我的sendNotification()方法有关?

IntentService IntentService

public class GcmIntentService extends IntentService {

public static final int NOTIFICATION_ID = 1;

private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

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

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        // Filter messages based on message type. Since it is likely that GCM will be extended in the future with 
        // new message types, just ignore any message types you're not interested in, or that you don't recognize.
        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());               
        }

        // If it's a regular GCM message, do some work.
        else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            for (int i=0; i<5; i++) {
                Log.i("GCM", "Working... " + (i+1) + "/5 @ " + SystemClock.elapsedRealtime());
                try {
                    Thread.sleep(5000);
                } 
                catch (InterruptedException e) { e.printStackTrace(); }
            }
            Log.i("GCM", "Completed work @ " + SystemClock.elapsedRealtime());

            // Post notification of received message.
            sendNotification("Received: " + extras.toString());
            Log.i("GCM", "Received: " + extras.toString());
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with a GCM message.
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, LoginActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

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

} }

BroadcastReceiver 广播接收器

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

Figured it out!!! 弄清楚了!!!

Turns out you need to have 3 things for every notification: 事实证明,每个通知都需要具备以下三点:

  1. setSmallIcon() setSmallIcon()
  2. setContentTitle() setContentTitle()
  3. setContextText() setContextText()

Doh! 卫生署!

From the look of your response on your server with the 'fake_message_I'd in, you must have set the dry_run parameter in your message. 从服务器上带有“ fake_message_I”的响应的外观来看,您必须在消息中设置了dry_run参数。 This just validates the message without actually sending it. 这只是验证消息而没有实际发送它。 Remove that parameter and retest. 删除该参数并重新测试。

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

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