1

我已經創建了一個活動,當通知發生時得到刷新,但我面臨的一個問題是,當通知來了,它繼續發送無盡的相同的通知。此前它工作正常,但我做了一些改變,這已經完成。請幫幫我。我附上了我的代碼。從android中的GCM意圖服務無休止地獲取通知android

代碼GCM IntentService類

  @Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    String msgg = intent.getStringExtra("message"); 

    final ResultReceiver receiver = intent.getParcelableExtra("receiver"); 
    Bundle bundle = new Bundle(); 
    if (!extras.isEmpty()) { 

     if (GoogleCloudMessaging. 
       MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 

      sendNotification(this, msgg); 


     } else if (GoogleCloudMessaging. 
       MESSAGE_TYPE_DELETED.equals(messageType)) { 

      sendNotification(this, msg); 
      updateMyActivity(this,msgg); 
      bundle.putString("result", msg); 
      receiver.send(STATUS_FINISHED, bundle); 

     } else if (GoogleCloudMessaging. 
       MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
      updateMyActivity(this,msgg); 
      sendNotification(this, msg); 



     } 


} 

發送通知代碼

private void sendNotification(Context context, String message) { 

    Intent resultIntent; 

    int icon = R.mipmap.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationCompat.Builder nBuilder; 
    Uri alarmSound = RingtoneManager 
      .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 


    NotificationCompat.BigPictureStyle notiStyle = new 
      NotificationCompat.BigPictureStyle(); 
    notiStyle.setBigContentTitle("afewtaps"); 
    notiStyle.setSummaryText(message); 


    nBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(icon) 
      .setContentTitle("afewtaps") 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setLights(Color.BLUE, 500, 500).setContentText(message) 
      .setAutoCancel(true).setTicker("Notification from afewtaps") 
      .setSound(alarmSound); 



    resultIntent = new Intent(context, 
      LoginActivity.class); 
    resultIntent.putExtra("message", message); 

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 


    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 
      notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    // Show the max number of notifications here 
    if (notify_no < 9) { 
     notify_no = notify_no + 1; 
    } else { 
     notify_no = 0; 
    } 
    nBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager nNotifyMgr = (NotificationManager) context 
      .getSystemService(context.NOTIFICATION_SERVICE); 



    nNotifyMgr.notify(notify_no + 2, nBuilder.build()); 


} 

信息發送到廣播

// This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with 
static void updateMyActivity(Context context, String message) { 

    Intent intent = new Intent("com.google.android.c2dm.intent.RECEIVE"); 

    //put whatever data you want to send, if any 
    intent.putExtra("message", message); 

    //send broadcast 
    context.sendBroadcast(intent); 
} 

這會結束intent類的代碼。

現在我的代碼活動的

  @Override 
public void onResume() { 
    super.onResume(); 
    // connectToDatabase(); 
    getActivity().registerReceiver(mMessageReceiver, new IntentFilter("com.google.android.c2dm.intent.RECEIVE")); 
} 

//Must unregister onPause() 
@Override 
public void onPause() { 
    super.onPause(); 
    getActivity().unregisterReceiver(mMessageReceiver); 
} 


//This is the handler that will manager to process the broadcast intent 
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     // Extract data included in the Intent 
     String message = intent.getStringExtra("message"); 

     //do other stuff here 

     connectToDatabase(); 


    } 
}; 
+0

現在GCM折舊您可以使用FCM的通知。 – Google

+0

@Google不,我需要使用GCM,因爲它處於結束水平。 –

+0

'notify_no'的值是什麼?在sendNotification方法中找不到它。 –

回答

1

好。因此,您的代碼發生了什麼事情,您正在向Receiver發送廣播,並強制onHandleIntent再次被調用,結果調用updateActivity方法,該方法再次廣播並循環連續不斷。

在你updateMyActivity方法,請更改此:

Intent intent = new Intent("com.google.android.c2dm.intent.RECEIVE"); 

Intent intent = new Intent("myMessage"); 

這裏的罪魁禍首是

com.google.android.c2dm.intent.RECEIVE 

廣播時調用onHandleIntent。

此外,在活動的onResume方法,請改變TAGmyMessage

getActivity().registerReceiver(mMessageReceiver, new IntentFilter("myMessage")) 
+0

此通知僅適用於第一次通知,但該通知僅適用於第一次通知 –