点击推送通知后打开活动的方法(Android)

6

我对Android编程一窍不通,如果这是一个简单的任务,那么请原谅。我基本上按照Vogella推送通知教程(http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html)来进行推送通知。我已经阅读了一些其他的stackoverflow问题,但是当我接收到通知后如何打开意图有点困惑。

例如,如果我只想让通知带我去一个网站,那该怎么办?它必须放在我的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);

}

}

2个回答

12

如果你想在用户点击通知时打开一个网站,请尝试以下操作:

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

    }

当我这样做时,在点击我的推送通知后,我只看到消息“收到新消息”。也许需要有些东西告诉pendingIntent打开意图? - Kevin
没事了,通过你的代码我已经解决了。只需要改一点点就好了。谢谢! - Kevin

0
在您的C2DM基础接收器或扩展基础接收器的类中,您有一个handleMessage()函数:
以下是处理消息并启动活动的示例代码:
@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());
        }
    }

1
抱歉,基础接收器是指消息接收器还是注册接收器?我假设该活动将放在扩展Activity的MessageReceivedActivity下。但它应该放在扩展Broadcast Receiver的MessageReceiver下吗? - Kevin
我编辑了我的问题,包括我的代码。因此,举个例子,假设我想要在点击通知时打开www.google.com?我知道如何使用Intent来实现,但不确定pendingIntent的工作原理。 - Kevin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接