如何获取每个通知的有效负载?
How to get payload of each and every notification?
我在我的应用程序中使用不同的消息 ID 来显示通知,当我单击通知时,如何获取每个通知的有效负载或消息以向用户显示相关信息。
multiple notificaion http://trip38.com/images/noti.png
一个好的参考或解决方法会更有帮助:)
我写的代码是
NotificationManager notifier = (NotificationManager) GCMIntentService.this.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(drawable, msg_alert, System.currentTimeMillis());
//Setup the Intent to open this Activity when clicked
Intent toLaunch = new Intent(GCMIntentService.this, MainActivity.class);
toLaunch.putExtra("msg", msg);
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, toLaunch, 0);
//Set the Notification Info
notification.tickerText = ticker_msg;
notification.setLatestEventInfo(GCMIntentService.this, noti_title, noti_msg, contentIntent);
//Setting Notification Flags
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notifier.notify(msg_id, notification);
编辑代码
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
private SharedPreferences mPreferences;
public GCMIntentService() {
super(AppConstants.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
mPreferences = context.getApplicationContext().getSharedPreferences(AppConstants.PREFS_FILE_NAME, 0);
SharedPreferences.Editor mEditor = mPreferences.edit();
mEditor.putString(AppConstants.REGISTRATION_KEY_STR, registrationId);
mEditor.apply();
//saved reg id in server
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Logger.d(TAG, "unregistered = " + arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Logger.d(TAG, "new message received: " + arg1.getStringExtra("message"));
showNotification(arg1.getStringExtra("message"));
}
@Override
protected void onError(Context arg0, String errorId) {
Logger.d(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
private void showNotification(String msg) {
String msg_alert = getResources().getString(R.string.app_name); //alert notification title
String noti_title = ""; //noti_title from msg
String noti_msg = ""; //noti_msg from msg
String ticker_msg = ""; //ticker_msg from msg
String msg_icon = ""; //msg_icon from msg
int msg_id; //msg_id from msg
//Get the icon for the notification
int drawable = R.drawable.ic_launcher;
if (!msg_icon.equals(""))
drawable = getResources().getIdentifier(msg_icon, "drawable", getPackageName());
if (drawable == 0) {
drawable = R.drawable.ic_launcher;
}
NotificationManager notifier = (NotificationManager) GCMIntentService.this.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(drawable, msg_alert, System.currentTimeMillis());
//Setup the Intent to open this Activity when clicked
Intent toLaunch = new Intent(GCMIntentService.this, MainActivity.class);
toLaunch.putExtra("msg_type", msg_type);
toLaunch.putExtra("trip_id", trip_id);
toLaunch.putExtra("booking_id", booking_id);
toLaunch.putExtra("noty_ticket_id", ticketId);
toLaunch.putExtra("msg", msg);
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, toLaunch, 0);
//Set the Notification Info
notification.tickerText = ticker_msg;
notification.setLatestEventInfo(GCMIntentService.this, noti_title, noti_msg, contentIntent);
//Setting Notification Flags
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notifier.notify(msg_id, notification);
}
}
这就是您在 activity MainActivity
中接收数据的方式。您将获得从通知传递到意图的有效负载。
String msgFromNotification = getIntent().getStringExtra("msg")
编辑
可能 msg
的值是静态的。代码见。如果你想收到通知上显示的消息
改变
toLaunch.putExtra("msg", msg);
到
toLaunch.putExtra("msg", noti_msg);
你的问题在这里:
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0,
toLaunch, 0);
如果您正在创建多个需要同时激活的 PendingIntent
,您需要确保它们是 "unique"。最简单的方法是在调用 PendingIntent.getActivity()
时使用不同的(唯一的)requestCode
参数。像这样:
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, msg_id,
toLaunch, 0);
(假设 msg_id
对于每条消息都是唯一的。如果不是,您可以使用其他一些唯一的数字。
我在我的应用程序中使用不同的消息 ID 来显示通知,当我单击通知时,如何获取每个通知的有效负载或消息以向用户显示相关信息。
multiple notificaion http://trip38.com/images/noti.png
一个好的参考或解决方法会更有帮助:)
我写的代码是
NotificationManager notifier = (NotificationManager) GCMIntentService.this.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(drawable, msg_alert, System.currentTimeMillis());
//Setup the Intent to open this Activity when clicked
Intent toLaunch = new Intent(GCMIntentService.this, MainActivity.class);
toLaunch.putExtra("msg", msg);
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, toLaunch, 0);
//Set the Notification Info
notification.tickerText = ticker_msg;
notification.setLatestEventInfo(GCMIntentService.this, noti_title, noti_msg, contentIntent);
//Setting Notification Flags
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notifier.notify(msg_id, notification);
编辑代码
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
private SharedPreferences mPreferences;
public GCMIntentService() {
super(AppConstants.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
mPreferences = context.getApplicationContext().getSharedPreferences(AppConstants.PREFS_FILE_NAME, 0);
SharedPreferences.Editor mEditor = mPreferences.edit();
mEditor.putString(AppConstants.REGISTRATION_KEY_STR, registrationId);
mEditor.apply();
//saved reg id in server
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Logger.d(TAG, "unregistered = " + arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Logger.d(TAG, "new message received: " + arg1.getStringExtra("message"));
showNotification(arg1.getStringExtra("message"));
}
@Override
protected void onError(Context arg0, String errorId) {
Logger.d(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
private void showNotification(String msg) {
String msg_alert = getResources().getString(R.string.app_name); //alert notification title
String noti_title = ""; //noti_title from msg
String noti_msg = ""; //noti_msg from msg
String ticker_msg = ""; //ticker_msg from msg
String msg_icon = ""; //msg_icon from msg
int msg_id; //msg_id from msg
//Get the icon for the notification
int drawable = R.drawable.ic_launcher;
if (!msg_icon.equals(""))
drawable = getResources().getIdentifier(msg_icon, "drawable", getPackageName());
if (drawable == 0) {
drawable = R.drawable.ic_launcher;
}
NotificationManager notifier = (NotificationManager) GCMIntentService.this.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(drawable, msg_alert, System.currentTimeMillis());
//Setup the Intent to open this Activity when clicked
Intent toLaunch = new Intent(GCMIntentService.this, MainActivity.class);
toLaunch.putExtra("msg_type", msg_type);
toLaunch.putExtra("trip_id", trip_id);
toLaunch.putExtra("booking_id", booking_id);
toLaunch.putExtra("noty_ticket_id", ticketId);
toLaunch.putExtra("msg", msg);
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0, toLaunch, 0);
//Set the Notification Info
notification.tickerText = ticker_msg;
notification.setLatestEventInfo(GCMIntentService.this, noti_title, noti_msg, contentIntent);
//Setting Notification Flags
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notifier.notify(msg_id, notification);
}
}
这就是您在 activity MainActivity
中接收数据的方式。您将获得从通知传递到意图的有效负载。
String msgFromNotification = getIntent().getStringExtra("msg")
编辑
可能 msg
的值是静态的。代码见。如果你想收到通知上显示的消息
改变
toLaunch.putExtra("msg", msg);
到
toLaunch.putExtra("msg", noti_msg);
你的问题在这里:
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0,
toLaunch, 0);
如果您正在创建多个需要同时激活的 PendingIntent
,您需要确保它们是 "unique"。最简单的方法是在调用 PendingIntent.getActivity()
时使用不同的(唯一的)requestCode
参数。像这样:
PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, msg_id,
toLaunch, 0);
(假设 msg_id
对于每条消息都是唯一的。如果不是,您可以使用其他一些唯一的数字。