我到处搜索,但没有找到任何作品。
我想在单击通知后启动特定活动,但该应用程序继续从 SplashActivity 启动。
这是我的代码...我开始变得疯狂尝试解决这个问题...
火力基地实施
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
private NotificationManager mNotificationManager;
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d(TAG, "Refreshed token: " + s);
sendResistrationToServer(s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null) {
Log.d(TAG, "Notification message was empty");
return;
}
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage);
}
private void sendResistrationToServer(String s) {
Preferences.set(Constants.Firebase.FIREBASE_APP_TOKEN, s);
}
private void showNotification(RemoteMessage remoteMessage) {
//Activity to be open
Intent i = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Titolo")
.setContentText("testo")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notificationBuilder.build());
}
}
我的清单
<application
android:name=".my.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".my.ui.activity.SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".my.ui.activity.MainActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />
<activity
android:name=".my.ui.activity.ProfileActivity"
android:label="@string/title_activity_modal_login" />
<activity
android:name=".my.ui.activity.LanguageSettingActivity"
android:label="@string/title_activity_modal_login" />
<activity
android:name=".my.ui.activity.NotificationActivity"
android:label="@string/title_activity_modal_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".my.base.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
我不明白问题是否在于我如何在FirebaseService内部或清单文件中调用活动。
使用这个
private void showNotification(RemoteMessage remoteMessage) {
//Activity to be open
Intent i = new Intent(this, NotificationActivity.class);
i .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Titolo")
.setContentText("testo")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notificationBuilder.build());
}