2014-10-11 83 views

回答

0

这是我在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)) { 
      Log.i(Consts.TAG, "onHandleIntent: message error"); 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
      Log.i(Consts.TAG, "onHandleIntent: message deleted"); 
     // If it's a regular GCM message, do some work. 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))) { 
      String subId = intent.getStringExtra(GCM_KEY_SUBID); 
      Log.e(Consts.TAG, "onHandleIntent: subId: " + subId); 
      String[] tokens = subId.split(":"); 
      String typeId = tokens[1]; 

      // dispatch message 
      if (GCM_TYPEID_QUERY.equals(typeId)) { 
       Intent messageIntent = new Intent(BROADCAST_ON_MESSAGE); 
       messageIntent.putExtras(intent); 
       messageIntent.putExtra("token", tokens[2]); 
       boolean isReceived = LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent); 
       //Check if the broadcast has been handled, if not show the notification. 
       if (!isReceived) { 
        Log.e(Consts.TAG, "A message has been recieved but no broadcast was handled."); 
        generateNotification(); 
       } else { 
        Log.e(Consts.TAG, "A message has been recieved, broadcasted and handled."); 
       } 
      } 
     } 
    } 
    // Release the wake lock provided by the WakefulBroadcastReceiver. 
    GCMBroadcastReceiver.completeWakefulIntent(intent); 
} 

public void generateNotification(){ 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.notification) 
        .setContentTitle("A new notification") 
        .setContentText("Some text explaining the notification"); 

    // all the gubbins needed to make the onclick of the notification open the main activity 
    Intent resultIntent = new Intent(); 
    resultIntent.setAction("com.company.projectname.action.LAUNCH_IT"); 
    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    // link the above onclick with the notification 
    mBuilder.setContentIntent(resultPendingIntent); 


    // Sets an ID for the notification 
    int mNotificationId = 001; 
    // Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    // Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
}; 
+0

我已经试过了,但对我不起作用代码工作,必须失去了一些东西 – Heli 2014-10-24 06:55:13

+0

请告诉我的错误? – EnduroDave 2014-10-24 06:56:21

+0

非常感谢您的回复 我没有收到任何错误, 它只是不显示任何通知提醒,其他所有工作都正常。 它在列表视图中显示消息。 看来GCMIntentService-onHandleIntent(...是从来没有所谓的触发 – Heli 2014-10-25 11:06:17