单击推送通知android后打开活动

单击推送通知android后打开活动

本文介绍了单击推送通知android后打开活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 编程一窍不通,很抱歉,如果这是一项简单的任务.我几乎遵循了推送通知的 Vogella 推送通知教程 (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html).我已经阅读了一些其他堆栈溢出问题,但我对如何在收到通知后打开意图感到有些困惑.

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.

例如,如果我只是想让通知将我引导至某个网站,那该怎么办?它是否必须放在我的 MessageReceivedActivity 或其他项目/类下?

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?

谢谢

这是我的 C2DMMessageReceiver 的代码

Here is the code I have for my 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);

}

}

推荐答案

如果您想在收到通知时打开网站,请点击试试这个:

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);

    }

这篇关于单击推送通知android后打开活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 13:01