Android 应用在点击通知时启动缓慢 [英] Android app launches slow on notification tap

本文介绍了Android 应用在点击通知时启动缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FCM 向我的应用发送推送通知;用户点击通知时的预期行为是正常启动应用程序,例如点击启动器应用程序图标.

I'm using FCM to send push notifications to my app; the expected behaviour when the user taps the notification is to launch the app as normal, like tapping the launcher app icon.

目前,当用户点击通知时,应用程序打开前需要等待 30 秒以上.通知消失了,但应用没有打开......甚至没有白屏,什么都没有.

At the moment, when user tap on the notification, spend more than 30 seconds before app is opened. Notification go away, but the app does not opens... Not even a white screen, nothing.

onMessageReceived被触发时,displayNotification方法被调用.

When onMessageReceived is triggered, displayNotification method is called.

private void displayNotification(String title, String body) {

    createNotificationChannel();
    PendingIntent contentIntent = generateNotificationContentIntent();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CONTENTS_CHANNEL_ID)
            .setSmallIcon(R.drawable.logo_no_bg_white)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(contentIntent)
            .setAutoCancel(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}

private PendingIntent generateNotificationContentIntent(){
    Intent intent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(intent);
    return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void createNotificationChannel() {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
        return;
    }

    NotificationChannel notificationChannel = new NotificationChannel(CONTENTS_CHANNEL_ID, CONTENTS_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
    notificationChannel.setDescription(CONTENTS_CHANNEL_ID);

    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(notificationChannel);

}

我的 onCreate MainActivity 方法只是检查用户是否已登录,如果是,则启动 HomeActivity:

My onCreate MainActivity method simply checks wether the user is logged, and if so, launches the HomeActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PACKAGE_NAME = getApplicationContext().getPackageName();

    //Read shared preferences.
    sharedPref = getSharedPreferences(PACKAGE_NAME, Context.MODE_PRIVATE);

    if(sharedPref.contains("token") || sharedPref.getBoolean("isGuest", false)){
        //Redirect to home
        Intent homeIntent = new Intent(getApplicationContext(), HomeActivity.class );
        homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(homeIntent);
        finish();
    }

    loadTowns(this);

}

我做错了什么?

更新:清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="my.awesome.package">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

<application
    android:allowBackup="false"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/icon"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.com.vansuita.pickimage.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:replace="android:authorities">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/picker_provider_paths" />
    </provider>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/logo_no_bg_white" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@android:color/holo_orange_dark" />

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginActivity"
        android:screenOrientation="portrait"
        android:theme="@style/NoActionBar" />
    <activity
        android:name=".HomeActivity"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".ChannelProfileActivity"
        android:label="@string/channel"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".TownProfileActivity"
        android:label="@string/town"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".ContentActivity"
        android:label="@string/content"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeActivity" />
    </activity>
    <activity
        android:name=".ContentsListActivity"
        android:label="@string/contents"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeActivity" />
    </activity>
    <activity
        android:name=".QuestionResultsActivity"
        android:label="@string/question"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeActivity" />
    </activity>
    <activity
        android:name=".ForgetPasswordActivity"
        android:label="@string/DidYouForgetPassword"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".ValidationProcesActivity"
        android:label="@string/verify_user"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeActivity" />
    </activity>
    <activity
        android:name=".QuestionVoteActivity"
        android:label="@string/question"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".HomeActivity" />
    </activity>
    <activity
        android:name=".NewUserActivity"
        android:label="@string/new_user"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".NewUserSuccessActivity"
        android:label="@string/new_user"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />

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

    <activity
        android:name=".NotificationsActivity"
        android:label="@string/notifications"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".ViewNotificationActivity"
        android:label="@string/notifications"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".NewFriendshipRequestActivity"
        android:label="@string/notifications"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />

    <activity
        android:name=".TypeUserSelectionActivity"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
    <activity
        android:name=".SelectTownActivity"
        android:screenOrientation="portrait"
        android:theme="@style/WhiteActionBarTheme" />
</application>

推荐答案

试试这个,它对我有用.

Try this, it is working for me.

Intent notificationIntent = new Intent(this, MainActivity.class);
        /*flags for resume activity*/
        notificationIntent.setFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP
                |Intent.FLAG_ACTIVITY_CLEAR_TASK
                |Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);
        /*for clear notification on click*/
        builder.setAutoCancel(true);

这篇关于Android 应用在点击通知时启动缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆