如何获得每个通知的有效负载?

时间:2015-01-14 15:19:14

标签: android push-notification google-cloud-messaging android-pendingintent

我使用不同的msg 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);
}

}

2 个答案:

答案 0 :(得分:0)

这是您在活动MainActivity中接收数据的方式。您将获得从通知传递到intent的有效负载。

String msgFromNotification =  getIntent().getStringExtra("msg")

修改

msg的值可能是静态的。看你的代码。如果您想收到通知上显示的消息

更改

toLaunch.putExtra("msg", msg);

toLaunch.putExtra("msg", noti_msg);

答案 1 :(得分:0)

你的问题在这里:

PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, 0,
                                         toLaunch, 0);

如果您要创建多个需要同时处于活动状态的PendingIntent,则需要确保它们是“唯一的”#34;。最简单的方法是在调用requestCode时使用不同的(唯一)PendingIntent.getActivity()参数。像这样:

PendingIntent contentIntent = PendingIntent.getActivity(GCMIntentService.this, msg_id,
                                         toLaunch, 0);

(这假定msg_id对于每封邮件都是唯一的。如果不是,您可以使用其他唯一的号码。