2014-10-11 4 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

+0

отсутствует Что ошибка? – EnduroDave

+0

Большое спасибо за ваш ответ Я не получаю никаких ошибок, он просто не отображает никаких уведомлений, все остальное работает нормально. отображает сообщения в виде списка. появляется, что GCMIntentService-onHandleIntent (... никогда не вызывается для инициирования уведомления – Heli

Смежные вопросы