簡體   English   中英

禁用僅在后台接收的通知

[英]Disable notifications to be received only in background

我使用firebase雲消息傳遞和firebase功能構建了一個聊天應用程序。 但我目前有兩個問題。 1.當應用程序處於打開狀態並且有新消息進入時,應用程序將自動移動到應用程序的主要活動。 2.其次,我希望只有在后台才能收到通知。

我該如何實現這一目標?

下面是我的onMessageReceived

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

        }
    }

    private void showNotification(String name, String click_action, String title) {
        Intent intent;

        if (click_action.equals("Download")) {
            intent = new Intent(this, Download.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else if (click_action.equals("Student_SystemsDevt")) {
            intent = new Intent(this, Student_SystemsDevt.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        else {
            intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(name)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

我的清單文件

<application
    android:name=".GTUCONLINE"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:screenOrientation="portrait"
        android:theme="@style/MyTheme" />
    <activity android:name=".MainActivity">
    </activity>
    <activity
        android:name=".ChoiceActivity"
        android:screenOrientation="portrait"
        android:theme="@style/MyTheme" />
    <activity
        android:name=".WelcomeActivity"
        android:screenOrientation="portrait"
        android:theme="@style/MyTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
        android:theme="@style/Base.Theme.AppCompat" />
    <activity
        android:name=".StudentSignUp"
        android:screenOrientation="portrait"
        android:theme="@style/MyTheme" />
    <activity
        android:name=".LecturerSignUp"
        android:screenOrientation="portrait" />
    <activity android:name=".ProgrammeActivity" />

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

    <activity
        android:name=".TopicActivity"
        android:theme="@style/MyTheme" />
    <activity android:name=".MIS" />
    <activity android:name=".GB" />
    <activity android:name=".FIN" />
    <activity android:name=".ENGINEERINGM" />
    <activity android:name=".BDM" />
    <activity android:name=".SCM" />
    <activity android:name=".TE" />
    <activity android:name=".TM" />
    <activity android:name=".BET" />
    <activity android:name=".ICT" />
    <activity android:name=".T" />
    <activity android:name=".IT" />
    <activity android:name=".AM" />
    <activity android:name=".OGM" />
    <activity android:name=".I" />
    <activity android:name=".PEF" />
    <activity android:name=".QM" />
    <activity android:name=".EPM" />
    <activity android:name=".PM" />
    <activity android:name=".HM" />
    <activity android:name=".StudentsList">
        <intent-filter>
            <action android:name="StudentsList" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".LecturersList" />
    <activity android:name=".AdminActivity" />
    <activity
        android:name=".ChatActivity"
        android:parentActivityName=".StudentsList" />
    <activity android:name=".LecturerMainActivity" />
    <activity
        android:name=".Download"
        android:parentActivityName=".MIS">
        <intent-filter>
            <action android:name="Download" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MIS_Information"
        android:parentActivityName=".MIS" />
    <activity android:name=".Terms" />
    <activity
        android:name=".Admin_Login"
        android:parentActivityName=".LoginActivity" />
    <activity
        android:name=".Manipulation"
        android:parentActivityName=".Admin_Login" />
    <activity android:name=".Admins" />
    <activity
        android:name=".SystemsDevt"
        android:parentActivityName=".MIS" />
    <activity
        android:name=".SysGroupChat"
        android:parentActivityName=".SystemsDevt" />
    <activity
        android:name=".EntSystems"
        android:parentActivityName=".MIS" />
    <activity android:name=".DownloadEnt">
        <intent-filter>
            <action android:name="DownloadEnt" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".student_mis" />
    <activity
        android:name=".Student_SystemsDevt"
        android:parentActivityName=".student_mis">
        <intent-filter>
            <action android:name="Student_SystemsDevt" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Student_EntSys"
        android:parentActivityName=".student_mis" />
    <activity
        android:name=".SystemsDevt_Add"
        android:parentActivityName=".SystemsDevt" />
    <activity android:name=".EntSystemsAdd" />
    <activity android:name=".EntSystemsInfo" />
    <activity
        android:name=".DatabaseSystems"
        android:parentActivityName=".MIS" />
    <activity android:name=".DownloadDatabaseSys" />
    <activity
        android:name=".Student_DatabaseSystems"
        android:parentActivityName=".student_mis" />
    <activity android:name=".DatabaseInfo" />
    <activity
        android:name=".DatabaseAdd"
        android:parentActivityName=".DatabaseSystems" />
    <activity
        android:name=".IntroToOil"
        android:parentActivityName=".GB" />
    <activity
        android:name=".DownloadIntro"
        android:parentActivityName=".IntroToOil" />
    <activity
        android:name=".IntroToOilInfo"
        android:parentActivityName=".IntroToOil" />
    <activity
        android:name=".IntroToOilAdd"
        android:parentActivityName=".IntroToOil" />
    <activity android:name=".student_gb" />
    <activity android:name=".StudentIntro" />
    <activity android:name=".MIS_students_new"
        android:parentActivityName=".SystemsDevt">

    </activity>
</application>

1-實際上onMessageReceived僅在您的應用程序處於前台時觸發,如此處所述輸入鏈接描述 因此,如果您不覆蓋onMessageReceived,則當您的應用處於前台時,您將無法接收推送

2-根據fcm docs,當你的應用程序處於后台時,不會觸發onMessageReceived

數據有效負載是在啟動器Activity的意圖的附加內容中提供的。

它解釋了為什么你的推送重定向到你的主要活動(你的發射器)。

到目前為止,您可以處理意圖的重定向。

您還可以在此處指定要重定向的活動,例如在此處輸入鏈接描述

{
  "to":"some_device_token",
  "content_available": true,
  "notification": {
    "title": "hello",
    "body": "test message",
    "click_action": "OPEN_ACTIVITY_1"
  },
  "data": {
    "extra":"juice"
  }
}

不要忘記所需活動的意圖過濾器:

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

還在清單中添加“任務”權限。

private boolean isAppForeground() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        final String packageName = getPackageName();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    } 

 if(isAppForeground()){
   // Handle notification silently without displaying in notification tray
 }else{
    // Do your regular stuff

   // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {

        }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM