簡體   English   中英

Android 發送一個信號通知時應用程序崩潰

[英]Android App Crash when One Signal Notification sent

每次我發送 One Signal Push 通知時,我的應用程序都會崩潰。 注意:firebase 沒有問題,也有一個信號 inApp 消息工作正常。 另外,接收一個崩潰信號的推送通知

使用 firebase,它沒有問題,但我想使用一個信號 我嘗試了很多東西但沒有用

這是我從 logcat 中得到的錯誤

錯誤

2021-07-20 22:10:37.431 14322-16493/###### E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
    Process: #######, PID: 14322
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getTitle()' on a null object reference
        at ######.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:17)
        at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging@@22.0.0:13)
        at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging@@22.0.0:8)
        at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging@@22.0.0:3)
        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging@@22.0.0:3)
        at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(com.google.firebase:firebase-messaging@@22.0.0:1)
        at com.google.firebase.messaging.EnhancedIntentService$$Lambda$0.run(Unknown Source:6)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.5.0:6)
        at java.lang.Thread.run(Thread.java:764)

這是我的代碼

我在清單文件中添加了 firebase class 和一個信號 class

Android 艙單

<?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="######"
    android:installLocation="auto">
    <!--
         <uses-sdk
         android:minSdkVersion="16"
         android:targetSdkVersion="26" />
    -->
    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true" />

    <application

        android:name=".AppController"
        android:allowBackup="false"
       
        >
        <activity android:name=".activity.MainActivity" />
        <activity
            android:name=".activity.SplashScreen"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
        <activity
            android:name=".activity.SettingActivity"
            android:label=""
            android:theme="@style/Theme.AppCompat.Light.Dialog" />
        <activity
            android:name=".activity.InstructionActivity"
            android:label="My Custom AppIntro"
            android:theme="@style/Theme.Intro" />

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/admob_app_id" />
        <meta-data
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true" />
        <meta-data
            android:name="com.google.android.gms.games.APP_ID"
            android:value="@string/app_id" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/notification_yellow" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

        <meta-data android:name="com.onesignal.NotificationAccentColor.DEFAULT"
            android:value="#FFCC00" />

        <activity android:name=".activity.ReviewActivity" />
        <activity android:name=".activity.BookmarkList" />
        <activity android:name=".AboutUs" />
        <activity android:name=".About" />

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


    </application>

</manifest>

建造。 Gradle(應用)

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
    compileSdkVersion 30
    buildToolsVersion "28.0.3"
    defaultConfig {

        applicationId "#########"
        minSdkVersion 17

        targetSdkVersion 30
        versionCode 1
        versionName "1.0.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        archivesBaseName = "Offline Quiz"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildFeatures {
        viewBinding true
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    implementation 'com.google.firebase:firebase-analytics:19.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.navigation:navigation-fragment:2.3.0'
    implementation 'androidx.navigation:navigation-ui:2.3.0'
    implementation 'com.google.firebase:firebase-inappmessaging-display:20.0.0'
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    // implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.google.android.gms:play-services-ads:19.4.0'
    implementation 'com.google.android.gms:play-services-plus:17.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'org.jsoup:jsoup:1.11.3'
    implementation 'com.google.android.gms:play-services-games:21.0.0'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    implementation 'com.github.ybq:Android-SpinKit:1.4.0'
    implementation 'com.google.android.gms:play-services-location:18.0.0'

    implementation 'com.github.AppIntro:AppIntro:6.1.0'
    implementation 'io.github.dreierf:material-intro-screen:0.0.6'

    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'de.hdodenhof:circleimageview:3.1.0'

    // One signal SDK
    implementation 'com.onesignal:OneSignal:4.4.1'

    // firebase
    implementation platform('com.google.firebase:firebase-bom:28.2.1')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging'

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

Build.Gradle(項目)

buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:0.13.4'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

    }
}

allprojects {
    repositories {

        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
        google()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Firebase class

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import org.jetbrains.annotations.NotNull;

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

        getFirebaseMessage(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

    }
    public void getFirebaseMessage (String title, String msg)
    {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"myFirebasechannel")
                .setSmallIcon(R.drawable.ic_notification_test)
                .setContentTitle(title)
                .setContentText(msg)
                .setAutoCancel(true);

        NotificationManagerCompat manger=NotificationManagerCompat.from(this);
        manger.notify(101,builder.build());
    }
}

這為我解決了問題: https://stackoverflow.com/a/45268868/9878385

我必須在app/build.gradle中添加implementation "com.google.firebase:firebase-core:16.0.1"或類似的

暫無
暫無

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

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