【问题标题】:Opening activity after clicking push notification android单击推送通知android后打开活动
【发布时间】:2012-04-25 13:18:26
【问题描述】:

我是 Android 编程的大菜鸟,如果这是一个简单的任务,我很抱歉。我非常关注 Vogella 推送通知教程的推送通知 (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html)。我已经阅读了其他一些堆栈溢出问题,但我对收到通知后如何打开意图有点困惑。

例如,如果我只是想让通知将我引导到一个网站,那该怎么做?它是否必须放在我的 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);

}

}

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    如果你想在通知上打开一个网站,请点击试试这个:

        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打开意图?
    • 没关系,通过您的代码弄清楚了。只需要改变一件小事。谢谢!
    【解决方案2】:

    在您的 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());
            }
        }
    

    【讨论】:

    • 对不起,基本接收者是指消息接收者还是注册接收者?我假设该活动将在扩展 Activity 的 MessageReceivedActivity 下进行。但它会在扩展广播接收器的 MessageReceiver 下进行吗?
    • 我编辑了我的问题以包含我的代码。因此,例如,假设我希望在单击通知时打开 www.google.com?我了解如何使用 Intent 进行操作,但不确定 pendingIntent 的工作原理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多