Android 通过 Sinch 推送通知,如何知道谁试图联系
Android push notification via Sinch, how to know who tried to contact
按照这个 tutorial 我可以创建推送通知,但是当我点击通知时它会打开消息应用程序但我不知道是谁给我发消息,我想知道是否有是一种了解发送者的方式,消息内容(如果可能)以及如果我单击通知是否可以直接与该人聊天。
为了查看 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﹕倾销意图开始
E/message﹕[from=551512823036]
E/message﹕[android.support.content.wakelockid=2]
E/message﹕[collapse_key=do_not_collapse]
E/message﹕倾销意图结束
但是我看不到sender_id
查看您收到的邮件,那里应该有一个发件人ID。
有关消息的文档,请参阅此处
http://download.sinch.com/docs/android/latest/reference/index.html
http://download.sinch.com/docs/android/latest/reference/com/sinch/android/rtc/messaging/Message.html
如果您使用教程中提供的示例后端 (https://github.com/sinch/push-backend-ruby),则不会将 sender_id 发送到服务器。您还需要更新后端逻辑以处理传入的 sender_id,并将其包含在 GCM 选项中。
按照这个 tutorial 我可以创建推送通知,但是当我点击通知时它会打开消息应用程序但我不知道是谁给我发消息,我想知道是否有是一种了解发送者的方式,消息内容(如果可能)以及如果我单击通知是否可以直接与该人聊天。
为了查看 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﹕倾销意图开始
E/message﹕[from=551512823036]
E/message﹕[android.support.content.wakelockid=2]
E/message﹕[collapse_key=do_not_collapse]
E/message﹕倾销意图结束
但是我看不到sender_id
查看您收到的邮件,那里应该有一个发件人ID。
有关消息的文档,请参阅此处 http://download.sinch.com/docs/android/latest/reference/index.html http://download.sinch.com/docs/android/latest/reference/com/sinch/android/rtc/messaging/Message.html
如果您使用教程中提供的示例后端 (https://github.com/sinch/push-backend-ruby),则不会将 sender_id 发送到服务器。您还需要更新后端逻辑以处理传入的 sender_id,并将其包含在 GCM 选项中。