Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My App Crash every time I send a One Signal Push notification. note: firebase has no issue also one signal inApp message working fine. also, Receive push notification of one signal with the crash

with firebase, it has no issue but I want to use one signal I tries so many things but not working

Here is my error from logcat

The Error

<pre lang="Java">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)

Here are my codes

I have added both the firebase class and one signal class in the manifest file

Android Manifest

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




Build. Gradle (app)

Java
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(project)

Java
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

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


please Help

What I have tried:

Tried to change @nullable but not working
Posted
Updated 4-Aug-23 2:21am
Comments
David Crow 21-Jul-21 8:32am    
In the statement:
getFirebaseMessage(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());

getNotification() is returning null.
Feather WoRK 21-Jul-21 11:52am    
yah can you tell me the solution?
David Crow 21-Jul-21 12:13pm    
Don't call getTitle() from a null object.

From the docs:
public RemoteMessage.Notification getNotification ()
Gets the notification data from the message if set.

This field will be non-null if a notification message is received while the application is in the foreground.

Returns
The message notification or null.

It crashes because of the
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String

and that is not something we can fix for you. You will need to use your debugger to find out exactly which reference is null and why.
 
Share this answer
 
I did some changes in Firebase class actually just copy-pasted from the example project which is given in firebase documentation and it works like charm but yes onMessagRecived will not work.
 
Share this answer
 
Comments
Member 16064767 4-Aug-23 9:03am    
can i see your script please? because i got same problem and i dont know where i must change to solve it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900