我正在尝试在我的工作地点从我的前任继承的Android项目中使用Firebase崩溃报告和通知。
由于某些未知原因,Firebase或OneSignal拒绝使用该项目,因此该应用程序是否处于后台并不重要。我使用Android Studio Assistant实现了Firebase。
但是,如果我创建新项目并实施Firebase或OneSignal,它将无故障地工作。
模块build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.myfirm.appname"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.android:facebook-android-sdk:4.20.0'
compile 'de.hdodenhof:circleimageview:1.3.0'
compile 'com.android.volley:volley:1.0.0'
compile 'com.squareup.picasso:picasso:2.5.0'
compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
compile 'com.android.support:design:25.3.0'
compile 'com.android.support:customtabs:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:cardview-v7:25.3.0'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.google.firebase:firebase-crash:10.0.1'
compile 'com.onesignal:OneSignal:3.4.3'
compile 'com.google.android.gms:play-services-gcm:10.0.1'
compile 'com.google.android.gms:play-services-location:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
项目build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
清单
<?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="com.myfirm.appname">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@drawable/notification_icon"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:node="replace">
<service
android:name=".services.TokenService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<meta-data android:name="onesignal_app_id" android:value="@string/onesignal_app_id" tools:replace="android:value"/>
<meta-data android:name="onesignal_google_project_number" android:value="@string/onesignal_google_project_number" tools:replace="android:value"/>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_appid" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="fbXXXXXXXXXXXX" />
</intent-filter>
</activity>
// More Activities
</application>
</manifest>
所有MyApplication
@Override
public void onCreate() {
super.onCreate();
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.DEBUG, OneSignal.LOG_LEVEL.NONE);
FirebaseApp.initializeApp(this);
// Removed some code
OneSignal.startInit(this)
.setNotificationReceivedHandler(new OneSignalMessageReceiver())
.init();
}
有几点需要注意:
当我发送通知时,应用程序始终处于后台(我启动了它并按下了主页按钮)。
在信息中心(Firebase和OneSignal)上,消息显示已成功发送。
我在Firebase上获取设备令牌,并使用它来发送通知。有时它会发送通知,有时则表示令牌未注册。
我之所以使用Firebase和OneSignal,是因为我首先尝试了Firebase,但它没有用。然后我使用OneSignal希望帽子它可以工作,但它没有。因此,如果有人工作,我会删除另一个。
ProjectName&gt;中存在google-services.json
app 是的,包名称与清单中的名称相对应。
即使应用程序已经崩溃,也不会在Firebase信息中心上显示崩溃报告。