Android - 从通知重定向主要 activity 中的片段
Android - redirect a fragment in main activity from notification
我有一个 loginActivity 和 MainActivity,其中包含多个片段,其中之一称为 NotificationFragment。
要求:假设用户已经登录并且当前在 Home Fragment(在 Main Activity 内)并且应用程序在后台。现在他们收到推送通知,用户应该被重定向到通知片段(在 Main Activity 上)。
截至目前,登录 Activity 正在从通知中接收意向数据。登录 Activity 是我们目前的发布 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" />
试试这个
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());
}
它将触发主activity。
添加
android:launchMode="singleTask" in your mainactivity to avoid running mutiple instance of MainActivity.
<activity
android:name=".MainActivity"
android:launchMode="singleTask" />
我有一个 loginActivity 和 MainActivity,其中包含多个片段,其中之一称为 NotificationFragment。
要求:假设用户已经登录并且当前在 Home Fragment(在 Main Activity 内)并且应用程序在后台。现在他们收到推送通知,用户应该被重定向到通知片段(在 Main Activity 上)。
截至目前,登录 Activity 正在从通知中接收意向数据。登录 Activity 是我们目前的发布 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" />
试试这个
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());
}
它将触发主activity。
添加
android:launchMode="singleTask" in your mainactivity to avoid running mutiple instance of MainActivity.
<activity
android:name=".MainActivity"
android:launchMode="singleTask" />