私は Android プログラミングに非常に慣れていないため、これが単純な作業である場合は申し訳ありません。プッシュ通知に関する Vogella プッシュ通知チュートリアル (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html) にほぼ従いました。他のスタック オーバーフローに関する質問をいくつか読みましたが、通知を受け取った後にインテントを開く方法について少し混乱しています。
たとえば、通知によって Web サイトに誘導したい場合、どのように機能しますか? 私の MessageReceivedActivity または別のプロジェクト/クラスの下に一緒に移動する必要がありますか?
ありがとう
C2DMMessageReceiver のコードは次のとおりです。
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("C2DM", "Received message");
final String payload = intent.getStringExtra("payload");
Log.d("C2DM", "dmControl: payload = " + payload);
// TODO Send this to my application server to get the real data
// Lets make something visible to show that we received the message
createNotification(context, payload);
}
}
public void createNotification(Context context, String payload) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Message received", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//adding LED lights to notification
notification.defaults |= Notification.DEFAULT_LIGHTS;
Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
notification.setLatestEventInfo(context, "Message",
"New message received", pendingIntent);
notificationManager.notify(0, notification);
}
}