即使正确指定了Intent,startActivity()也不会执行任何操作

时间:2016-11-28 10:43:15

标签: android android-activity push-notification firebase-cloud-messaging start-activity

我正在尝试在点击FCM生成的通知时从启动器活动中打开一个活动。即使执行上面写的日志语句,startActivity函数也不执行任何操作。以下是一些相关的文件/函数,请说明为什么ConversationDetailActivity无法启动:

从LoginActivity开始另一项活动的功能

void handleNotificationClick()
{
    if (getIntent().getExtras() != null)
    {
        for (String key : getIntent().getExtras().keySet())
        {
            Object value = getIntent().getExtras().get(key);
            System.out.println("----- pushNotif Key: " + key + " Value: " + value);
        }

        Intent intent;

        if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_CATEGORY).equals(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
        {
            intent = new Intent(LoginActivity.this, ConversationDetailActivity.class);
        }
        else
        {
            intent = new Intent(LoginActivity.this, ApprovalDetailActivity.class);
            if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_APPROVAL_TYPE).equals("I"))
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
            else if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_APPROVAL_TYPE).equals("A"))
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
            else
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);

            intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
        }
        intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID));
        intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
        intent.putExtra(Master.KEY_POSITION, 0);

        System.out.println("----- starting new activity from handleNotificationClick");
        LoginActivity.this.startActivity(intent);
        LoginActivity.this.finish();
    }
}

生成通知的sendNotification函数

private void sendNotification(String messageBody)
    {
        Intent intent;
        System.out.println("----message body: " + messageBody);
        if(notificationBundle.getCategory().equalsIgnoreCase(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
        {
            intent = new Intent(this, ConversationDetailActivity.class);
        }
        else
        {
            intent = new Intent(this, ApprovalDetailActivity.class);
            if(notificationBundle.getApprovalType().equals("I"))
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
            else if(notificationBundle.getApprovalType().equals("A"))
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
            else
                intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);

            intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
        }

        intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , notificationBundle.getPostID());
        intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
        intent.putExtra(Master.KEY_POSITION, 0);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.mnet_icon)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        int random = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(random, notificationBuilder.build());
    }

的AndroidManifest.xml

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name=".MnetApplication"
        android:allowBackup="true"
        android:icon="@drawable/mnet_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activities.LoginActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_main"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar" />

        <activity
            android:name=".activities.ConversationDetailActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_conversation_detail"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTask"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <intent-filter>
                <action android:name=".activities.ConversationDetailActivity" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
        </activity>
        <activity
            android:name=".activities.ApprovalDetailActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_approval_detail"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
        </activity>
        <activity
            android:name=".activities.NewConversationActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_new_conversation"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
        </activity>
        <activity
            android:name=".activities.NotificationActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_notification"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
        </activity>
        <activity
            android:name=".activities.ProfileActivity"
            android:label="@string/title_activity_profile"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
        </activity>

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

        <service
            android:name=".utils.firebase.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

    </application>

</manifest>

4 个答案:

答案 0 :(得分:0)

尝试将 android:exported =“true” 添加到AndroidManifest.xml中的活动

答案 1 :(得分:0)

首先完成登录活动,然后调用开始活动。

LoginActivity.this.finish();
LoginActivity.this.startActivity(intent);

答案 2 :(得分:0)

在您的清单中,定义您要打开的类

  <activity
        android:name=".activities.ApprovalDetailActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@style/MyMaterialTheme"
        android:windowSoftInputMode="stateHidden">

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

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


    </activity>

答案 3 :(得分:-1)

对我来说什么都没有。对我来说,事情很简单。确保将其添加到要直接打开的活动中。

        <intent-filter>
            <action android:name="MainActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

并且必须从推送通知中添加新的有效负载: click_action 看起来像这样-

"notification": {
  "title": "hello",
  "body": "test message",
  "click_action": "MAIN_ACTIVITY"
},
  

注意:您可以根据需要 MAIN_ACTIVITY 对其进行命名。