1

I am using the firebase cloud messaging to get the push notification. It was working fine. But now whenever the app receives the notification data, it crashes and doesn't even show the line number on which it crashes.

I have already checked the code of FirebaseMessagingService Class of my another application in which the notification service is working perfectly fine.

It shows the following error in logs

E/AndroidRuntime: FATAL EXCEPTION: Firebase-WrappedFirebaseMessagingService
Process: net.eazypg.eazypgtenant, PID: 16823
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
    at com.google.firebase.messaging.zzb.<init>(Unknown Source:5)
    at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:58)
    at com.google.firebase.iid.zzb.run(Unknown Source:2)
    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(Unknown Source:6)
    at java.lang.Thread.run(Thread.java:764)

My Firebase Messeging class is

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private NotificationManager notifManager;
private NotificationChannel mChannel;

FirebaseAuth firebaseAuth;
FirebaseDatabase firebaseDatabase;
FirebaseUser firebaseUser;
DatabaseReference databaseReference;

@Override
public void onNewToken(String s) {
    super.onNewToken(s);

    sendRegistrationToServer(s);

}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Map<String, String> data = remoteMessage.getData();

    pushNotification(data.get("title"),
                        data.get("body"),
                        data.get("imageUrl"),
                        data.get("click_action"));

}

private void pushNotification(String title, String body, String imageUrl, String clickAction) {

    Intent intent;
    PendingIntent pendingIntent;
    NotificationCompat.Builder builder;

    if (notifManager == null) {
        notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    intent = new Intent(clickAction);
    intent.setAction(Long.toString(System.currentTimeMillis()));

    int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


        if (mChannel == null) {
            NotificationChannel mChannel = new NotificationChannel("0", title, NotificationManager.IMPORTANCE_DEFAULT);
            mChannel.setDescription (body);
            mChannel.enableVibration (true);
            mChannel.setVibrationPattern (new long[] {100, 200, 300, 400, 300, 200, 400});
            notifManager.createNotificationChannel (mChannel);
        }

        builder = new NotificationCompat.Builder (this, "0");

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentTitle(title)  // flare_icon_30

                .setSmallIcon(R.drawable.notification)
                .setContentText(body)
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_logo))
                .setBadgeIconType(R.drawable.icon_logo)
                .setContentIntent(pendingIntent)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[]{100, 200, 300, 400, 300, 200, 100, 300});

    } else {

        builder = new NotificationCompat.Builder(this, "0");

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentTitle(title)
                .setSmallIcon(R.drawable.icon_logo) // required
                .setContentText(body)  // required
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_logo))
                .setContentIntent(pendingIntent)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[]{100, 200, 300, 400, 300, 200, 100, 300})
                .setPriority(Notification.PRIORITY_HIGH);
    }

    Notification notification = builder.build ();
    notifManager.notify (0, notification);

}

} I have already initialized everything required in Manifest file

<meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

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

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

Thank you very much in advance!

Leonardo Velozo
  • 598
  • 1
  • 6
  • 16
Vardaan
  • 101
  • 1
  • 5
  • 1
    posible duplicate of https://stackoverflow.com/questions/28515049/android-content-context-getpackagename-on-a-null-object-reference – coroutineDispatcher Apr 25 '19 at 13:13
  • 1
    or this : https://stackoverflow.com/questions/43571622/sending-push-notification-with-firebase-fatal-exception-android – coroutineDispatcher Apr 25 '19 at 13:14
  • u r getting a null poniter on package name .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_logo)) use R.mipmap – Mayur Apr 25 '19 at 13:15

1 Answers1

0

try updating your gradle with the lastest firebase messaging

implementation 'com.google.firebase:firebase-messaging:17.5.0'

here is a similar issue Application crash upon receiving a notification

Master Fathi
  • 349
  • 1
  • 9
  • 19