android - 单击推送通知android后打开 Activity

标签 android push-notification

我是 Android 编程的 super 菜鸟,如果这是一项简单的任务,我深表歉意。我几乎遵循了推送通知的 Vogella 推送通知教程 (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html)。我读过其他一些堆栈溢出问题,但我对如何在收到通知后打开 Intent 感到有点困惑。

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

}

最佳答案

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

    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 - 单击推送通知android后打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308710/

相关文章:

Android webview 电话 :0000 could not be loaded because net:ERR

parse-platform - 解析推送通知 : Another Error Occurred

android - 设备停止接收 GCM 推送,直到网络切换

php - 苹果推送通知问题

Android 访问外部存储

android - getBluetoothLeAdvertiser() 返回 null

安卓工作室 : UnsatisfiedLinkError: findLibrary returned null - loading native library

java - 更改数组中的值不会传播到应用程序中

ios - firebase通知cordova ios集成

php - Laravel Push Notifications,静态调用的非静态方法