繁体   English   中英

Android FCM通知未在API 25-上推送

[英]Android FCM Notification does not push up on API 25-

我正在尝试将Firebase云消息传递添加到我的项目中。 问题是当设备处于睡眠状态或应用程序处于后台时,我想推送通知。 Firebase文档说,当您不在应用程序处于前台时发送通知时,系统将完成此工作,但是为什么它可以在API 26而不是更低版本的API 25上运行呢?

前台应用程序中的通知由我处理,并且工作正常,我的目标API是26-编译的SDK 26.0.2,并使用最新版本的FCM pkg。

我的测试设备是具有API 19、22、25、26的adbs和具有API 25,Samsung S8 API 26的Xperia X

我发送的通知JSON:

    {
    "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
        }
    }

我的清单:

    <?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>

我的gradle文件:

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'

发送通知时,这是Log Cat Firebase:

    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

非常感谢Radek的每一个回答。

就您而言,如果您的应用程序处于后台或处于终止状态,Firebase将不会调用onMessageReceived()

要在后台或处于终止状态下获取通知,请使用data对象而不是notification对象。

下面将工作:

{
"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
}

在清单文件中添加以下内容

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

创建OnBootBroadcastReceiver类,然后调用Firebase服务。

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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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