启动器活动改变了这就是为什么FCM推送通知在后台应用程序时不起作用的原因

时间:2017-03-03 22:51:20

标签: android push-notification notifications android-notifications firebase-cloud-messaging

我已经更改了我的启动器活动,而不是.MainActivity。当我的应用程序在后台时,Firebase推送通知服务不起作用,但当应用程序位于前台时,它可以正常工作。

我在我的应用中添加了WelcomeSlider,这就是为什么我必须将欢迎滑块保持为启动器活动的原因。我检查过,如果我将启动器活动更改为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 });
    }

}

1 个答案:

答案 0 :(得分:2)

我已经自己解决了这个问题。我只是将MainActivity作为Android Manifest的启动器活动保持

<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();
        }
它对我有用! :) :)

相关问题