单击推送通知android后打开活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10308710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening activity after clicking push notification android
提问by Kevin
I am a huge noob to Android programming so sorry if this is a simple task. I pretty much followed the Vogella push notification tutorial for push notifications (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html). I've read some other stack overflow questions but I'm a little confused on how to open a intent once I receive the notification.
我是 Android 编程的一个大菜鸟,如果这是一项简单的任务,我很抱歉。我几乎遵循了推送通知的 Vogella 推送通知教程 (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html)。我已经阅读了一些其他堆栈溢出问题,但是我对收到通知后如何打开意图感到有些困惑。
For example, if I just wanted the notification to lead me to a website, how would that work? Would it have to go under my MessageReceivedActivity or another project/class all together?
例如,如果我只是想让通知将我引导到一个网站,那将如何工作?它是否必须放在我的 MessageReceivedActivity 或另一个项目/类下?
Thanks
谢谢
Here is the code I have for my C2DMMessageReceiver
这是我的 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);
}
}
}
回答by Zaz Gmy
if you want to open a website on notification click try this:
如果您想在通知时打开网站,请单击尝试:
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("android.intent.action.VIEW",
Uri.parse("http://my.example.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
notification.setLatestEventInfo(context, "Message",
"New message received", pendingIntent);
notificationManager.notify(0, notification);
}
回答by Shankar Agarwal
In your base receiver for c2dm or the class the that extentd base reciever you have a handleMessage()::
在 c2dm 的基本接收器或扩展基本接收器的类中,您有一个 handleMessage()::
Below is the sample code for handle message which launches the activity::
以下是启动活动的处理消息的示例代码:
@Override
protected void handleMessage(Context context, Intent intent) {
String regId = C2DMessaging.getRegistrationId(context);
String logKey = this.getClass().getSimpleName();
String title="";
String message="";
if (regId!= null) {
if (intent.hasExtra(Constants.TITLE)) {
title = intent.getStringExtra(Constants.TITLE);
}
if(intent.hasExtra(Constants.MESSAGE)){
message = intent.getStringExtra(Constants.MESSAGE);
}
// TODO Send this to my application server to get the real data
// Lets make something visible to show that we received the message
if(!title.equals("") && !message.equals(""))
createNotificationForMsg(context,title,message);
}
}
@Override
public void createNotificationForMsg(Context context,String title,String message) {
final String logKey = this.getClass().getSimpleName();
try {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"update(s) received", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//adding sound to notification
notification.defaults |= Notification.DEFAULT_SOUND;
Intent intent = new Intent(context, YourAlertActivity.class);
if(Constants.LOG)Log.d(logKey, Constants.TITLE +": "+ title +" , "+Constants.MESSAGE+": "+message);
intent.putExtra(Constants.TITLE, title);
intent.putExtra(Constants.MESSAGE, message);
PendingIntent pendingIntent = PendingIntent.getActivity(context, Calendar.getInstance().get(Calendar.MILLISECOND), intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, "Castrol",
title+"update Received", pendingIntent);
notificationManager.notify(Calendar.getInstance().get(Calendar.MILLISECOND), notification);
} catch (Exception e) {
// MessageReceivedActivity.view.setText("createNotificationFor Msg: "
// + e.toString());
}
}