谷歌云消息,接收消息,但不建立通知

Posted

技术标签:

【中文标题】谷歌云消息,接收消息,但不建立通知【英文标题】:Google Cloud Messaging, receiving message, but not building notification 【发布时间】:2014-04-15 04:39:09 【问题描述】:

我已在我的应用上实现了适用于 android 的 Google Cloud Messaging 客户端。从我们的第 3 方服务器发送时,我们收到一条成功消息:

已编辑

事实证明,我实际上是从 Google Cloud Messaging 服务器接收消息,但我没有在手机上收到通知。我知道我收到了这条消息,因为它显示了我的 Logcat。唯一的问题是我的通知没有建立/发送。

为什么会这样?请注意我的GcmIntentService 是一个单独的类。以及BroadcastReceiver。我猜这与我的sendNotification() 方法有关?

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());

广播接收器

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);



【问题讨论】:

您在检查注册时获取设备 ID 吗? androidhive.info/2012/10/… @Saqib 是的,我得到registration id 没有任何问题。 看起来不错,但只是尝试一下,更改它&lt;service =".GCMIntentService" android:enabled="true" /&gt; [注意大写的 GCM] 【参考方案1】:

想通了!!!

结果你需要为每个通知准备 3 件事:

    setSmallIcon() setContentTitle() setContextText()

哇!

【讨论】:

【参考方案2】:

从您在服务器上使用 'fake_message_I'd 的响应来看,您必须在消息中设置了 dry_run 参数。这只是验证消息而不实际发送它。删除该参数并重新测试。

【讨论】:

我也发现了这个并重新测试,但仍然没有出现在我的手机上。

以上是关于谷歌云消息,接收消息,但不建立通知的主要内容,如果未能解决你的问题,请参考以下文章

适用于 Android 谷歌云消息 (GCM) 的 Azure 移动服务从不向设备发送通知

用于监控谷歌云 pubsub 中未传递消息的 REST API

谷歌云消息通知图标小

用户之间的 Android 谷歌云消息传递

用户多台设备上的谷歌云消息通知

无需谷歌云消息(GCM)将通知从 PC 推送到 android