遵循本教程后,我能够创建推送通知,但是当我单击通知时,它会打开消息传递应用程序,但我不知道是谁向我发送了消息,我想知道是否有办法知道是谁发送的,如果可能的话,消息的内容,如果我点击通知,它可以直接与那个人聊天。
要查看 GCM 发送的内容,我将以下内容添加到 gcmintentservice :
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(intent);
}
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, LoginActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Message!");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void handleMessage(Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
Log.e("message","Dumping Intent start");
while (it.hasNext()) {
String key = it.next();
Log.e("message","[" + key + "=" + bundle.get(key)+"]");
}
Log.e("message","Dumping Intent end");
}
}`
}
我得到的日志消息是:
E/message﹕Dumping Intent start
E/message:[来自=551512823036]
E/message:[android.support.content.wakelockid=2]
E/message:[collapse_key=do_not_collapse]
E/message﹕Dumping Intent end
但我看不到 sender_id