android通知从Activity打开片段

时间:2014-12-02 06:12:13

标签: android notifications

大家好,我的应用程序需要生成通知。

为此我做了

 private void generateNotification(String message) {
            mNotificationManager = (NotificationManager) this
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    this)
                    /* .setSmallIcon(R.drawable.ic_launcher) */
                    .setSmallIcon(R.drawable.app_icon)
                    .setContentTitle(getResources().getString(R.string.app_name))
                    .setStyle(
                            new NotificationCompat.BigTextStyle().bigText(message))
                    .setTicker(message).setContentText(message).setAutoCancel(true);

            // Play default notification sound
            mBuilder.setDefaults(Notification.DEFAULT_SOUND
                    | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS);

            /*
             * PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new
             * Intent(this, RecentChatList.class), 0);
             */
            /*
             * PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new
             * Intent(this, DashboardActivity.class), 0);
             */
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, DashboardActivity.class), 0);
            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }

并进入DashboardActivity

if (Global.getOPEN_WHICH_FRAGMENT_ON_NOTIFICATION().contains(
                "SINGLE MESSAGE")) {
            Global.setOPEN_WHICH_FRAGMENT_ON_NOTIFICATION("");
            FragmentManager fragmentManager1;
            // on first time display view for first item
            Fragment fragment1 = null;
            fragment1 = new ChatFragment();
            if (fragment1 != null) {
                fragmentManager1 = getFragmentManager();
                fragmentManager1.beginTransaction()
                        .replace(R.id.frmLayout, fragment1).commit();

                txtHeaderTitle.setText("Chat");
            }
        }
        else
        {
            Global.setOPEN_WHICH_FRAGMENT_ON_NOTIFICATION("");
            FragmentManager fragmentManager1;
            // on first time display view for first item
            Fragment fragment1 = null;
            fragment1 = new MyGroupsFragment();
            if (fragment1 != null) {
                fragmentManager1 = getFragmentManager();
                fragmentManager1.beginTransaction()
                        .replace(R.id.frmLayout, fragment1).commit();

                txtHeaderTitle.setText("Group Chat");
            }
        }

这个工作正常,意味着当单个聊天通知到达时它会打开正确的片段(chatfragment)但是当组通知它再次打开聊天片段时不会打开我在此代码中遗漏的组片段

提前致谢

Pragna

0 个答案:

没有答案