2014-03-05 129 views
5

我正在開發一個android應用程序,我需要接收多個通知,每個通知都包含唯一的數據。如何在點擊推送通知時打開新的活動?

事情我已經完成 1.使用GCM服務 2.顯示數據上點擊一個新的活動通知

的問題發送給服務器接收多個通知: 想我已經收到了三個通知,每個都有唯一的數據。當我點擊一個通知時,一個新的活動開始於該通知中的數據。所以現在接收通知活動正在運行。現在,如果我單擊第二個通知接收通知活動加載舊數據(第一個通知中的數據)。如果我關閉應用程序並單擊第三個通知,則接收通知活動會加載第三個通知中的數據,因爲它是支持的。

我曾嘗試:

  1. 設置意圖標誌FLAG_ACTIVITY_CLEAR_TOP(不工作)
  2. 編輯清單文件中包括機器人:launchMode = 「singleInstance」 沒有任何成功

我正在使用

resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

In intentservice and add清單文件中的都不起作用。

intentservice類

public class GcmIntentService extends IntentService{ 

Context context; 
public static int notify_no=0; 

//System.currentTimeMillis(); 

private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 
public static final String TAG = "GCM NOTIFICATION"; 

public GcmIntentService() { 
    super("GcmIntentService"); 
    // TODO Auto-generated constructor stub 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 
    Bundle extras = intent.getExtras(); 
    String msg = intent.getStringExtra("message"); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 


    if (!extras.isEmpty()) { 

     if (GoogleCloudMessaging. 
        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
       // sendNotification(RegIdDTO.REG_ID,"Send error: " + extras.toString()); 
      sendNotification(this,msg); 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_DELETED.equals(messageType)) { 
       // sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " + 
       //   extras.toString()); 
       sendNotification(this,msg); 
      // 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(TAG, "Working... " + (i+1) 
          + "/5 @ " + SystemClock.elapsedRealtime()); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
        } 
       } 
       Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
       // Post notification of received message. 
       //sendNotification("Received: " + extras.toString()); 
       // sendNotification(RegIdDTO.REG_ID,msg); 
       sendNotification(this,msg); 
       Log.i(TAG, "Received: " + extras.toString()); 
      } 
     } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 


private static void sendNotification(Context context,String message) { 
    int icon = R.drawable.ic_stat_gcm; 
    long when = System.currentTimeMillis(); 
    NotificationCompat.Builder nBuilder; 
    Uri alarmSound = RingtoneManager 
      .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    nBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("header") 
      .setLights(Color.BLUE, 500, 500).setContentText(message) 
      .setAutoCancel(true).setTicker("Notification from Traffic") 
      .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 }) 
      .setSound(alarmSound) 
      ; 

    String consumerid = null; 
    Integer position = null; 
      // write your click event here 
     Intent resultIntent = new Intent(context, NotificationReceiveActivity.class); 

     resultIntent.putExtra("message", message); 
     // resultIntent.setData(Uri.parse("content://"+when)); 
     resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_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()); 
    } 

} 

接收活動

public class NotificationReceiveActivity extends Activity { 

    TextView name; 
    TextView deal; 
    TextView valid; 
    TextView address; 
    JSONObject json; 
    GcmIntentService serv; 
    Context mContext; 
    // static boolean active = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_notification_receive); 


    Intent intent = getIntent(); 

    name = (TextView) findViewById(R.id.name); 
    deal = (TextView) findViewById(R.id.deal); 
    valid = (TextView) findViewById(R.id.valid); 
    address = (TextView)findViewById(R.id.address); 
    String message = intent.getExtras().getString("message"); 

    try { 
     json = new JSONObject(message); 
     String stime = json.getString("name"); 
     name.setText(stime); 

     String slecturename = json.getString("deal"); 
     deal.setText(slecturename); 

     String sroom = json.getString("valid"); 
     valid.setText(sroom); 

     String sfaculty = json.getString("address"); 
     address.setText(sfaculty); 


    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    serv=new GcmIntentService(); 
    //serv.CancelNotification(getApplicationContext()); 

} 
} 

使用上面的代碼中,我能夠收到通知,但問題是,如果我的行爲是不是在運行狀態下,我收到了通知。此時如果我從通知中打開活動,它會顯示新的數據,但是如果活動正在運行並且同時有通知到達。如果我打開此通知,那麼它將使用舊數據加載活動。即使有活動運行,我也想在活動中顯示新數據。

+0

所以你想在通知到達時自動打開活動,我是否正確? – Kedarnath

+0

會發生什麼?當我點擊一個新的通知時,一個新的活動開始於通過該通知收到的數據。所以,點擊通知並開始新的活動工作正常。 我面臨的問題是當我嘗試打開一個新的通知,並且活動已經在運行時:活動只顯示舊數據(來自之前的通知)。如果我關閉應用程序並打開通知,它工作正常。 – Durga

+0

明白了,所以如果活動沒有運行,並且它已經在運行,你想要顯示新的數據。正確嗎? – Kedarnath

回答

2

當您獲得新的意向並且您的活動使用的是singleTop或意圖使用的是FLAG_ACTIVITY_SINGLE_TOP時,您的新意圖實際上會被傳送到活動的onNewIntent。引用文檔:

這就是所謂的,在他們的包設置launchMode爲「singleTop」,或者如果一個客戶端調用startActivity(意向)時使用的FLAG_ACTIVITY_SINGLE_TOP旗活動。在這兩種情況下,當活動在活動堆棧頂部重新啓動時,而不是啓動活動的新實例時,將使用用於重新啓動的Intent在現有實例上調用onNewIntent()它。

在接收新的意圖之前,活動總是會暫停,因此您可以指望在此方法之後調用onResume()。

請注意getIntent()仍然返回原始的Intent。你可以使用setIntent(Intent)將它更新到這個新的Intent。

因此,如果您在NotificationReceiveActivity中覆蓋該方法,應該解決您的問題。

+0

你能修改我的代碼嗎? – Durga

0

覆蓋您的活動中的onNewIntent

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    intent.getExtra("message", message); 
} 
相關問題