2016-12-29 121 views
2

我創建時發生的通知,但我現在面臨一個問題,就是我每次都得到了通知,但我的活動僅刷新一次,或當我做我的裝置解鎖該得到刷新一個活動。每當我收到任何通知時,我都無法刷新活動。我在活動中實施了廣播。通知僅適用於第一次

代碼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("unique_name"); 

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

//send broadcast 
context.sendBroadcast(intent); 

}

此結束對意圖類代碼。

現在我的活動代碼

  @Override 
     public void onResume() { 
      super.onResume(); 
      // connectToDatabase(); 
      getActivity().registerReceiver(mMessageReceiver, new            IntentFilter("unoque_name")); 

}

  //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

的通知工作,因爲你是通過它在後臺運行的IntentService送他們。

不能使用這種方法來更改活動什麼。這是因爲活動並不總是在運行,它會被破壞,它的生命週期會暫停。因此,相應的接收機也不總是運行。

一個解決方案是,您可以在IntentService中執行更新,並在活動啓動時,您可以在onResume()中重新加載。

+0

我也做了同樣 –

+0

我想我是不太清楚。我的意思是說你的活動和與之相關的廣播接收機不會一直運行。這意味着該活動永遠不會得到更新。 我提出的解決方案是,您在意向服務中獲取數據庫更新,使用共享首選項保存數據,並且當用戶啓動活動時,活動從共享首選項加載數據。 – theawless