简体   繁体   中英

Android FCM Notification does not push up on API 25-

I am trying to add firebase cloud messaging to my project. The problem is that I would like to push up notifications when the device is sleeping or the application is in background. Firebase documentation says, that when you send notification when app is not in the foreground, system will do the job but why it works on API 26 and not on API 25 lower?

Notifications in foreground app are handled by me and it works fine, my targeted API is 26 - compiled SDK 26.0.2 and using the newest version of FCM pkg.

My testing devices are adbs with API 19,22,25,26 and Xperia X with API 25, Samsung S8 API 26

My notification JSON that I send:

    {
    "to" : "existing key",
    "condition" : null,
    "collapse_key" : null,
    "priority" : "high",
    "content_available" : null,
    "time_to_live" : 3600,
    "restricted_package_name" : null,
    "dry_run" : null,
    "data" : null,
    "registration_ids" : null,
    "notification" : {
        "title" : "ViLo Testzor",
        "body" : "Alarm. ",
        "sound" : null,
        "icon" : null,
        "tag" : null,
        "color" : null,
        "badge" : null,
        "bodyLocKey" : null,
        "bodyLockArgs" : null,
        "clickAction" : null,
        "titleLocKey" : null,
        "titleLocArgs" : null
        }
    }

My manifest:

    <?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="cz.pkg.app">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />


    <application
        android:name=".android.App"
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:minSdkVersion="19"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppFullScreenTheme">
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_app_icon" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel"
            android:value="@string/default_notification_channel_id" />
        <activity
            android:name=".android.ActivityMain"
            android:hardwareAccelerated="true"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:theme="@style/AppFullScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".android.SenzasenFirebaseInstanceIdService"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".android.SenzasenMessagingService"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service
            android:name=".android.BackgroundWatcher"
            android:enabled="true"
            android:exported="true" />

        <receiver
            android:name=".android.notifications.BroadcastRecieverUpdater"
            android:enabled="true"
            android:exported="true"></receiver>
    </application>

</manifest>

My gradle file :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "cz.pkg.app"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:26.0.0-alpha1'
    compile 'com.google.android.gms:play-services-maps:11.4.2'
    compile 'com.google.firebase:firebase-core:11.4.2'
    compile 'com.google.firebase:firebase-messaging:11.4.2'
    testCompile 'junit:junit:4.12'
}


apply plugin: 'com.google.gms.google-services'

This is log cat Firebase when notification is send:

    10-05 12:25:04.444 5568-5568/? I/FA: App measurement is starting up, version: 11400
10-05 12:25:04.444 5568-5568/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
10-05 12:25:04.450 5568-5568/? V/FA: Collection enabled
10-05 12:25:04.450 5568-5568/? V/FA: App package, google app id: cz.pkg.app, 1:535056224042:android:2977b77751aa5b1f
10-05 12:25:04.451 5568-5568/? I/FA: To enable faster debug mode event logging run:
                                       adb shell setprop debug.firebase.analytics.app cz.pkg.app
10-05 12:25:04.451 5568-5568/? D/FA: Debug-level message logging enabled
10-05 12:25:04.455 5568-5568/? V/FA: Cancelling job. JobID: 717907879
10-05 12:25:04.456 5568-5568/? V/FA: Registered activity lifecycle callback
10-05 12:25:04.464 5568-5590/? V/FA: Using measurement service
10-05 12:25:04.464 5568-5590/? V/FA: Connecting to remote service
10-05 12:25:04.471 5568-5590/? V/FA: Using measurement service
10-05 12:25:04.471 5568-5590/? V/FA: Connection attempt already in progress
10-05 12:25:04.475 5568-5568/? V/FA: onActivityCreated
10-05 12:25:04.527 5568-5590/cz.pkg.app V/FA: Using measurement service
10-05 12:25:04.527 5568-5590/cz.pkg.app V/FA: Connection attempt already in progress
10-05 12:25:04.529 5568-5590/cz.pkg.app V/FA: Activity resumed, time: 57001950
10-05 12:25:04.533 5568-5590/cz.pkg.app I/FA: Tag Manager is not found and thus will not be used
10-05 12:25:04.534 5568-5590/cz.pkg.app D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:04.547 5568-5590/cz.pkg.app V/FA: Using measurement service
10-05 12:25:04.547 5568-5590/cz.pkg.app V/FA: Connection attempt already in progress
10-05 12:25:04.599 5568-5590/cz.pkg.app D/FA: Connected to remote service
10-05 12:25:04.599 5568-5590/cz.pkg.app V/FA: Processing queued up service tasks: 4
10-05 12:25:04.618 7477-5602/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:04.621 7477-5602/? V/FA-SVC: Saving event, name, data size: screen_view(_vs), 57
10-05 12:25:04.622 7477-5602/? V/FA-SVC: Event recorded: Event{appId='cz.pkg.app', name='screen_view(_vs)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]}
10-05 12:25:04.623 7477-5602/? V/FA-SVC: Upload scheduled in approximately ms: 1458892
10-05 12:25:04.625 7477-5602/? V/FA-SVC: Cancelling job. JobID: 812057698
10-05 12:25:04.625 7477-5602/? V/FA-SVC: Scheduling upload with AlarmManager
10-05 12:25:04.626 7477-5602/? V/FA-SVC: Background event processing time, ms: 8
10-05 12:25:08.146 5568-5590/cz.pkg.app V/FA: Recording user engagement, ms: 3619
10-05 12:25:08.148 5568-5590/cz.pkg.app V/FA: Activity paused, time: 57005569
10-05 12:25:08.152 5568-5590/cz.pkg.app D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3619, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:08.205 7477-5602/? V/FA-SVC: Logging event: origin=auto,name=user_engagement(_e),params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3619, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]
10-05 12:25:08.210 7477-5602/? V/FA-SVC: Saving event, name, data size: user_engagement(_e), 67
10-05 12:25:08.210 7477-5602/? V/FA-SVC: Event recorded: Event{appId='cz.pkg.app', name='user_engagement(_e)', params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3619, firebase_screen_class(_sc)=ActivityMain, firebase_screen_id(_si)=2278851524854650625}]}
10-05 12:25:08.212 7477-5602/? V/FA-SVC: Upload scheduled in approximately ms: 1455303
10-05 12:25:08.213 7477-5602/? V/FA-SVC: Cancelling job. JobID: 812057698
10-05 12:25:08.214 7477-5602/? V/FA-SVC: Scheduling upload with AlarmManager
10-05 12:25:08.214 7477-5602/? V/FA-SVC: Background event processing time, ms: 9
10-05 12:25:15.012 5707-5707/? I/FA: App measurement is starting up, version: 11400
10-05 12:25:15.012 5707-5707/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
10-05 12:25:15.022 5707-5707/? V/FA: Collection enabled
10-05 12:25:15.022 5707-5707/? V/FA: App package, google app id: cz.pkg.app, 1:535056224042:android:2977b77751aa5b1f
10-05 12:25:15.024 5707-5707/? I/FA: To enable faster debug mode event logging run:
                                       adb shell setprop debug.firebase.analytics.app cz.pkg.app
10-05 12:25:15.024 5707-5707/? D/FA: Debug-level message logging enabled
10-05 12:25:15.033 5707-5707/? V/FA: Cancelling job. JobID: 717907879
10-05 12:25:15.039 5707-5707/? V/FA: Registered activity lifecycle callback
10-05 12:25:15.059 5707-5726/? V/FA: Using measurement service
10-05 12:25:15.061 5707-5726/? V/FA: Connecting to remote service
10-05 12:25:15.074 5707-5726/? V/FA: Using measurement service
10-05 12:25:15.074 5707-5726/? V/FA: Connection attempt already in progress
10-05 12:25:15.078 5707-5726/? D/FA: Connected to remote service
10-05 12:25:15.079 5707-5726/? V/FA: Processing queued up service tasks: 2
10-05 12:25:20.114 5707-5726/cz.pkg.app V/FA: Inactivity, disconnecting from the service

thank you very much for every response Radek.

In your case, Firebase will not call onMessageReceived() if your app is in the background or it is in the killed state.

To get the notification in the background or in the killed state, make the use of data object instead of notification object.

Below will work:

{
"to" : "existing key",
"condition" : null,
"collapse_key" : null,
"priority" : "high",
"content_available" : null,
"time_to_live" : 3600,
"restricted_package_name" : null,
"dry_run" : null,
"data" :  {
    "title" : "ViLo Testzor",
    "body" : "Alarm. ",
    "sound" : null,
    "icon" : null,
    "tag" : null,
    "color" : null,
    "badge" : null,
    "bodyLocKey" : null,
    "bodyLockArgs" : null,
    "clickAction" : null,
    "titleLocKey" : null,
    "titleLocArgs" : null
    },
"registration_ids" : null,
"notification" :null
}

Add below in Manifest File

<receiver
            android:name=".OnBootBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
 </receiver>

Create OnBootBroadcastReceiver Class, and call the Firebase service.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("Com.example.SenzasenMessagingService");
        i.setClass(context, SenzasenMessagingService.class);
        context.startService(i);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM