安卓 : Click notification to launch activity

标签 android notifications google-cloud-messaging android-pendingintent

如何区分 Notifications 并将它们传递给不同的 Activity ? 在下面的方法中,第三个 parameter(String tag) 值是从包含一个值的 onMessage() 中检索的。 根据该值,我想区分打开哪个 URL,例如 Google 或 Facebook。 在我将 Intent 代码包含在下面的“if”条件中后,该应用程序终止。 请帮忙

    private static void generateNotification(Context context, String message,
        String tag) {
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            message, when);
    String title = context.getString(R.string.app_name);

    Log.d("GCM", "Tag Value " + tag);

        if (tag.equals("one")) {
            // pass to a different activity
                       notificationIntent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("https://www.google.com"));

        }

        if (tag.equals("two")) {
            // pass to a different activity
                    notificationIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://www.facebook.com"));
        }


    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);

}

最佳答案

在 list 上为 Activity 创建 Intent 过滤器,您需要像这样启动

<activity
        android:name=".activities.TabsActivity"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="com.dittoadvertising.push" >
            </action>
            <category android:name="android.intent.category.DEFAULT" >
            </category>
        </intent-filter>

当 Uri.parse("https://www.facebook.com") = 过滤器中的操作名称

并且在你的activity中会调用方法onNewIntent(Intent newIntent),然后intent传入

关于安卓 : Click notification to launch activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12855395/

相关文章:

当设备处于 sleep 模式时,Android GCM 通知不会出现

java - Gson 和 Volley 的 Proguard 问题

android - Android 的 ListView 过滤器

ruby-on-rails - Rails faye实时通知系统

android - 从 AsyncTask 更新下载进度条到通知区域

php - 当我的应用程序关闭时,GCM 消息不会出现在我的应用程序中

android - 使用 AWS SNS 将仅通知负载发送到 GCM

android - .mov 文件将在浏览器中播放,但不会在 native MediaPlayer 应用程序中播放

java - 如何在一个字符串中存储多个单词而不替换java中的旧单词

ios - 离线本地通知(Swift)