我正在创建一个Android应用,我想在其中显示推送通知。我已经实现了c2dm的代码。
但它只是提供注册ID,但没有显示通知。
我正在使用以下代码:
活动中的:
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", "andrew.housego@gmail.com");
registrationIntent.setPackage("com.google.android.gsf");
startService(registrationIntent);
在收件人中:
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
System.out.println("action is " + action);
Log.w("C2DM", "Registration Receiver called");
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action))
{
Log.w("C2DM", "Received registration ID");
registrationId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
Log.d("C2DM", "dmControl: registrationId = " + registrationId + ", error = " + error);
String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
sendRegistrationIdToServer(deviceId, registrationId);
}
else if ("com.google.android.c2dm.intent.RECEIVE".equals(action))
{
handleMessage(context, intent);
createNotification(context, registrationId);
}
}
public void createNotification(Context context, String registrationId)
{
URL url;
try
{
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification =
new Notification(R.drawable.icon, "Registration successful",
System.currentTimeMillis());
String notificationTitle = "notification";
String notificationText = "New Notification from Bingo Diary";
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, BingoDiaryActivity.class);
intent.putExtra("registration_id", registrationId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
//notification.setLatestEventInfo(context, "Registration",
// "Successfully registered", pendingIntent);
notificationManager.notify(99, notification);
notification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
有谁告诉我哪里可能是问题? 感谢
答案 0 :(得分:0)
问题可能在于发送方。当您从服务器发送消息时,您会得到什么?