当应用处于后台/未运行时,未在通知单击时打开特定活动

时间:2016-11-25 12:11:58

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

只有在打开应用程序并执行通知单击时,通知单击才会启动指定的活动。如果应用程序处于后台/未运行且执行了通知单击,则会打开应用程序的MainActivity。简而言之,就像应用程序在活动堆栈之后正常打开而不是在PendingIntent中打开指定的活动。

我想根据用户的类型将通知点击重定向到两个不同的活动(ApprovalDetailActivity和ConversationDetailActivity)。

我正在使用FCM进行推送通知。我在这里粘贴我的Manifest文件和我的FCMListener文件。请帮帮我。

MyFirebaseMessagingService.java中的

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);
            /*Conversation conversation = Master.notificationBundle.getConversation();
            Master.conversationsList = new ArrayList<>();
            Master.conversationsList.add(conversation);*/
        }
        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);

        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="mnet.mediaware.com.m_net">

    <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="mnet.mediaware.com.m_net.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>

6 个答案:

答案 0 :(得分:6)

根据Firebase云消息传递文档 - 如果Activity在前台,则将调用onMessageReceived。如果活动处于后台或已关闭,则通知中心会显示应用启动器活动的通知消息。 For More information Check this link

答案 1 :(得分:1)

我想我得到了你的确切答案。

TaskStackBuilder Usage and Definition

请不要忽视上面链接中的精彩视频。

TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(getContext());
taskStackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

答案 2 :(得分:0)

从启动器活动的意图获取捆绑(用于推送通知数据有效负载),然后开始您的特定活动。

答案 3 :(得分:0)

您的应用程序在后台发送的通知消息。在这种情况下,通知将传递到设备的系统托盘。用户点击通知会默认打开应用启动器。

在后台接收时带有通知和数据有效负载的消息。在这种情况下,通知将传递到设备的系统托盘,并且 数据有效载荷是根据启动器活动的意图提供的

if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()){
    String value = getIntent().getExtras().getString(key);
    Log.d(TAG, "Key: " + key + " Value: " + value);
}}

使用此代码获取意图数据

答案 4 :(得分:0)

当应用程序处于后台Firebase推送通知数据有效负载中时,

因此,在“启动器活动”中,您只需检查并打开适当的屏幕即可

 if (intent.hasExtra("data")) {

    }

答案 5 :(得分:-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 对其进行命名。