gpt4 book ai didi

android - 启动器 Activity 已更改,这就是 FCM 推送通知在应用程序处于后台时不起作用的原因

转载 作者:太空狗 更新时间:2023-10-29 13:09:43 30 4
gpt4 key购买 nike

我已经更改了我的 Launcher Activity 而不是 .MainActivity。当我的应用程序在后台时,Firebase 推送通知服务不工作,但当应用程序在前台时它工作正常。

我在我的应用程序中添加了一个 WelcomeSlider,这就是为什么我必须将欢迎 slider 保留为启动器 Activity 的原因。我检查过,如果我将启动器 Activity 更改为 NotifyActivity,那么它会再次正常工作。

这是我的AndroidManifest.xml

<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".WelcomeActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
>

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />

<activity
android:name=".Vlog"
android:parentActivityName=".MainActivity" />

<activity android:name=".NotifyActivity"></activity>

<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>

这是 myFirebaseInstanceIdService:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh(){
String token = FirebaseInstanceId.getInstance().getToken();
Log.d("TOKEN",token);
}

}

这是 myFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

Intent intent = new Intent(this,NotifyActivity.class);


if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Bundle bundle = new Bundle();
bundle.putString("url",url);
intent.putExtras(bundle);
}

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.mipmap.notifyicon);
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(sound);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());

notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
}

}

最佳答案

我自己解决了这个问题。我只是将 MainActivity 保留为 Android Manifest 中的启动器 Activity,例如

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
>

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

并在 MainActivity onCreate 中发布下面的代码,以检查它是否是第一次启动!

//Check if it first time launching..
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);

if (isFirstRun) {
//show start activity

startActivity(new Intent(MainActivity.this, WelcomeActivity.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}

对我有用! :) :)

关于android - 启动器 Activity 已更改,这就是 FCM 推送通知在应用程序处于后台时不起作用的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589785/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com