我在通知管理器中意图调用一个变量,因为第一次意图成功运行但第二次当我意图新消息时,活动显示thr perivous值plz帮助我实际上是一个大问题
- 这是通知管理员的代码
醇>
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
@Override
protected void onRegistered(Context context, String 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
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean 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);
}
private static void generateNotification(Context context, String message) {
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 intent =
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);
}
}
- 在receivemessage class
醇>
public class receivemessage extends Activity{
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());
}
}
提前致谢
答案 0 :(得分:7)
替换此行
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent,0);
与
PendingIntent contentIntent = contentIntent = PendingIntent.getActivity(context,
(int) (Math.random() * 100), notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
答案 1 :(得分:4)
在第1部分中,您要添加此值:
notificationIntent.putExtra("activate",message.toString());
虽然在receivemessage类中(顺便说一下,错误的命名,类名应该是camel case)你有:
Intent i=getIntent();
saveit = i.getStringExtra("run");
也许你应该在那里:
saveit = i.getStringExtra("activate");
我们的想法是,根据您发布的内容,不清楚是否有任何组件实际提供此run
意图字符串。
编辑使用您的代码,以便从活动触发上述通知管理器:
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 = "NONE@!";
}
lblIntentExtra.setText(value);
}
}
通知正常,当点击通知我得到预期值。代码中唯一的区别是我使用的是getApplicationContext()
而不是您上面提到的上下文,但我不确定它的相关性。也许你可以比较差异,看看你做错了什么......
答案 2 :(得分: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,notification);
不要使用0。
希望这有帮助。
答案 3 :(得分: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);
}