【问题标题】:Android - redirect a fragment in main activity from notificationAndroid - 从通知重定向主要活动中的片段
【发布时间】:2018-07-23 15:42:15
【问题描述】:

我有一个带有多个片段的 loginActivity 和 MainActivity,其中一个称为 NotificationFragment。

要求:假设用户已经登录并且当前在 Home Fragment 上(在 Main Activity 内)并且应用程序在后台。现在他们收到推送通知,用户应该被重定向到通知片段(在主活动上)。

截至目前,Login Activity 正在接收来自通知的意图数据。登录活动是我们目前的启动活动。

问题:Login Activity(在后台)应该如何通知 Main Activity 将用户从 Home Fragment 重定向到 Notification Fragment?

清单如下所示:

<activity
        android:name="com.X.LoginActivity"
        android:configChanges="orientation"
        android:icon="@mipmap/ic_launcher"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>

            <!-- Sets the intent action to view the activity -->
            <action android:name="android.intent.action.VIEW" />
            <!-- Allows the link to be opened from a web browser -->
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Allows the deep link to be used without specifying the app name -->
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>
    <activity
        android:name="com.X.MainActivity"
        android:configChanges="orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustPan" />

【问题讨论】:

  • 如果用户登录,那么您的登录活动收到的意图数据?如果 Mainactivity 收到它,您可以使用 FragmentTransaction 移动到片段
  • LoginActivity 是我们的启动活动。
  • 如果用户已登录,则无需再次启动登录活动?你用过闪屏吗?
  • 我明白,但正如我所说,LoginActivity 是我们的默认启动器活动,正在接收推送通知。编辑了我的问题以包括清单。我们没有 SplashActivity。

标签: android android-fragments android-push-notification


【解决方案1】:

试试这个

 private void sendMyNotification(String title,String message) {

    //On click of notification it redirect to this Activity
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.mipmap.ic_appicon);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"Default")
            .setSmallIcon(R.drawable.ic_noti_new)
            .setLargeIcon(icon)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

它将触发主要活动。

添加

android:launchMode="singleTask" 在您的 mainactivity 中避免运行 MainActivity 的多个实例。

 <activity
        android:name=".MainActivity"
        android:launchMode="singleTask" />

【讨论】:

  • 欢迎提供帮助..!编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多