2013-10-16 19 views
1

中的一個活動我將Intenting通知管理器中的變量作爲第一次意圖成功工作,但第二次當我意圖新消息活動顯示thr perivous值plz幫助我出去im真是一個大問題如何將變量從notificationmanager傳遞給android

  1. 這裏是通知管理器的代碼

    公共類GCMIntentService擴展GCMBaseIntentService {

    私有靜態最後絃樂TAG = 「GCMIntentService」;

    公共GCMIntentService(){

    super(SENDER_ID); 
    

    }

    @Override 保護無效onRegistered(上下文語境,字符串registrationId){

    Log.i(TAG, "Device registered: regId = " + registrationId); 
    
    displayMessage(context, "Your device registred with GCM"); 
    
    Log.d("NAME", MainActivity.name); 
    
    ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId); 
    

    }

    @Override

    保護無效onUnregistered(上下文語境,字符串registrationId){

    Log.i(TAG, "Device unregistered"); 
    
    displayMessage(context, getString(R.string.gcm_unregistered)); 
    
    ServerUtilities.unregister(context, registrationId); 
    

    }

    @Override 保護無效的onMessage(上下文範圍內,意圖意圖){

    Log.i(TAG, "Received message"); 
    
    String message = intent.getExtras().getString("price"); 
    
    displayMessage(context, message); 
    
    // notifies user 
    
    generateNotification(context, message); 
    

    }

    @Override protected void onDeletedMessages(C ontext上下文中,INT總){

    Log.i(TAG, "Received deleted messages notification"); 
    
    String message = getString(R.string.gcm_deleted, total); 
    
    displayMessage(context, message); 
    
    // notifies user 
    
    generateNotification(context, message); 
    

    }

    @Override 公共無效的onError(上下文語境,字符串ErrorID中){

    Log.i(TAG, "Received error: " + errorId); 
    
    displayMessage(context, getString(R.string.gcm_error, errorId)); 
    

    }

    @覆蓋 保護布爾onRecoverableError(Context context,String errorId){

    // log message 
    
    Log.i(TAG, "Received recoverable error: " + errorId); 
    
    displayMessage(context, getString(R.string.gcm_recoverable_error, 
         errorId)); 
    
    return super.onRecoverableError(context, errorId); 
    

    }

    私有靜態無效generateNotification(上下文語境,字符串消息){

    int icon = R.drawable.orange_logo; 
    
    long when = System.currentTimeMillis(); 
    
    NotificationManager notificationManager = (NotificationManager) 
         context.getSystemService(Context.NOTIFICATION_SERVICE); 
    
    Notification notification = new Notification(icon, message, when); 
    
    String title = context.getString(R.string.app_name); 
    
    Intent notificationIntent = new Intent(context,receivemessage.class); 
    
    // set intent so it does not start a new activity 
    
    notificationIntent.putExtra("activate",message.toString()); 
    
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
         Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    

    的PendingIntent意圖= 的PendingIntent。getActivity(context,0,notificationIntent,0);

    notification.setLatestEventInfo(context, title, message, contentIntent); 
    
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    
    // Play default notification sound 
    
    notification.defaults |= Notification.DEFAULT_SOUND; 
    
    // Vibrate if vibrate is enabled 
    
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    
    notificationManager.notify(0, notification);  
    

    }

}

  1. 在receivemessage類

公共類receivemessage延伸活動{

TextView textshow; 
String saveit; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.message); 

    textshow=(TextView)findViewById(R.id.showmessage); 


    Intent i=getIntent(); 
    saveit = i.getStringExtra("run"); 
    textshow.setText(saveit.toString()); 
} 

在此先感謝

回答

6

替換該行

PendingIntent intent = 
     PendingIntent.getActivity(context, 0, notificationIntent,0); 

PendingIntent contentIntent = contentIntent = PendingIntent.getActivity(context, 
        (int) (Math.random() * 100), notificationIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT); 
+0

好吧,讓我試試這個..... –

+0

它不工作的傢伙....... –

+0

WORKED FOR ME ..... – nobalG

3

在第1號要添加此值:

notificationIntent.putExtra("activate",message.toString()); 

而在receivemessage類(順便說一下,不好的命名,類名應該是駱駝)你有:

Intent i=getIntent(); 
saveit = i.getStringExtra("run"); 

也許你應該有:

saveit = i.getStringExtra("activate"); 

這個想法是,從你發佈的內容來看,目前還不清楚是否有任何組件實際上提供了這個run intent string extra。

編輯使用你的代碼,使上述通知管理器從活動觸發:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     findViewById(R.id.btn_some_action).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       setupClick(); 
      } 
     }); 
    } 

    private void setupClick() { 
     String message = "Sample notification"; 
     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     String title = getString(R.string.app_name); 

     Intent notificationIntent = new Intent(this, MySoActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.putExtra("run", message.toString()); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent intent = PendingIntent.getActivity(this.getApplicationContext(), 0, 
       notificationIntent, 0); 
     notification.setLatestEventInfo(this.getApplicationContext(), title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 
    } 

} 

,結果在MySoActivity類:

public class MySoActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_so_layout); 
     TextView lblIntentExtra = (TextView) findViewById(R.id.lblIntentExtra); 
     Intent intent = getIntent(); 
     String value = intent.getStringExtra("run"); 
     if (TextUtils.isEmpty(value)) { 
      value = "[email protected]!"; 
     } 
     lblIntentExtra.setText(value); 
    } 

} 

該通知把正常的和當點擊通知時,我獲得了預期的價值。代碼中的唯一區別是我使用的是getApplicationContext()而不是上面的上下文,但我不確定這是多麼相關。也許你可以比較的差異,看看你做錯了......

+0

我不好我已經改正了的事情,但它仍然給了我同樣的響應 –

+0

@ user2345589:編輯答案迴應你的編輯 – gunar

+0

sry dude它不會工作,因爲它是一個GCMBaseIntentService,它不能讓我使用getapplicationcontext –

0

在下面的代碼你generatenotification()方法的使用:

private void generateNotification(Context context, String message, String query) { 

    int icon = R.drawable.icon; 
     long when = System.currentTimeMillis(); 
    String appname = context.getResources().getString(R.string.app_name); 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notification; 

    Intent intent = new Intent(context, myActivity.class); 


    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      intent, 0); 

      NotificationCompat.Builder builder = new NotificationCompat.Builder(
        context); 
      notification = builder.setContentIntent(contentIntent) 
        .setSmallIcon(icon).setTicker(appname).setWhen(when) 
        .setAutoCancel(true).setContentTitle(appname) 
        .setContentText(message).build(); 

      notificationManager.notify((int) when, notification); 


    } 

Jus參見:notificationManager.notify((int)when ,通知);

不要使用0

希望這有助於。

0

這段代碼適合我, 試試這個。

替換此代碼:

在GCMIntentService.java替換這個 「generateNotification」 的方法。

GCMIntentService.java

private static void generateNotification(Context context, String message) { 

    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name);   

    Intent notificationIntent = new Intent(context, NotificationReceiver.class); 

    notificationIntent.putExtra("Notice", message); 
    notificationIntent.setAction(Intent.ACTION_VIEW); 
    notificationIntent.setAction("myString"+when); 
    notificationIntent.setData((Uri.parse("mystring"+when))); 

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    PendingIntent intent = PendingIntent.getActivity(context, (int) when, notificationIntent, 0);   

    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify((int) when, notification); 


} 

Receivemessage.java

TextView textshow; 

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.message); 

    textshow=(TextView)findViewById(R.id.showmessage); 

    Intent in = getIntent();   
    String text = in.getStringExtra("Notice"); 
    textshow.setText(text); 
}