NotificationManager尝试在空对象引用上调用虚拟方法'java.lang.String android.content.Context.getPackageName()'

时间:2019-03-05 16:09:38

标签: android

在开发环境中,创建通知通道没有问题。但是在qa / prod版本中,创建通知通道会导致以下异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.app.NotificationManager.createNotificationChannels(NotificationManager.java:488)
    at android.app.NotificationManager.createNotificationChannel(NotificationManager.java:476)
    at net.ugbb.***.service.FirebaseMessagingService.createNotificationChannel(Unknown Source:53)
    at net.ugbb.***.service.FirebaseMessagingService.onMessageReceived(Unknown Source:38)
    at com.google.firebase.messaging.FirebaseMessagingService.zzl(Unknown Source:47)
    at com.google.firebase.messaging.FirebaseMessagingService.zzJ(Unknown Source:96)
    at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source:65)
    at com.google.firebase.iid.zzb$zza$1.run(Unknown Source:24)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:784)

这是用于创建NotificationChannel的代码片段

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getBaseContext() != null ? getString(R.string.channel_name) : "";
        String description = getBaseContext() != null ? getString(R.string.channel_description) : "";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        if (notificationManager != null)
            if (getApplicationContext() != null && getApplicationContext().getPackageName() != null)
                notificationManager.createNotificationChannel(channel);
    }
}

我们正在使用IBM Maas360 MDM打包我们的应用程序并分发给我们的企业客户。

有什么解决方案可以解决这个问题,或者至少可以解决此问题吗?

更新:

onMessageReceived的代码

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);

    if (!(powerManager != null && powerManager.isInteractive())) {
        // TODO check documentation for changing deprecated constant FULL_WAKE_LOCK
        PowerManager.WakeLock wakeLock = powerManager != null ? powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "fiona:myLock") : null;
        if (wakeLock != null) {
            wakeLock.acquire(10*60*1000L /*10 minutes*/);
        }
    }

    sendNotification(remoteMessage.getData());
}

private void sendNotification(Map<String, String> data) {
    Intent intent = null;
    if (null != data) {
        String dataType;
        if (data.containsKey(ENotifElements.DATA_TYPE_KEY.value()))
            dataType = data.get(ENotifElements.DATA_TYPE_KEY.value());
        else
            return;

        switch (dataType) {
            case DIGITALLIZATION_EQUIPMENT:
                intent = handleDigitalizationProvisioningNotification(data);
                break;
            case NOTIF_TYPE_PROVISIONING:
                intent = handleDeviceProvisioningNotification(data);
                break;
            case TASK_LIST_CHANGED:
                intent = handleTaskListChangedNotification(data);
                break;
            case SIGNED_DOCUMENT_COMPLETION:
                intent = signedDocumentCompletion(data);
                break;
            case DIGITALIZATION_TASK_COMPLETE:
                intent = digitalizationTaskComplete(data);
                break;
            case SALES:
                salesNotification(data);
                break;
            default:
                intent = new Intent(this, WorkordersActivity.class);
                break;
        }

    }

    // Form notification item
    prepareNotificationContent(intent, data);
}

    private void prepareNotificationContent(Intent intent, Map<String, String> data) {
    if (intent != null)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, UNIQUE_INT_PER_CALL++, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    if(data !=null) {
        if (data.containsKey(ENotifElements.DATA_TYPE_KEY.value())) {
            String dataType = data.get(ENotifElements.DATA_TYPE_KEY.value());
            if (dataType.equals(SALES)) {
                pendingIntent = PendingIntent.getActivity(this, UNIQUE_INT_PER_CALL++, intent, PendingIntent.FLAG_ONE_SHOT);
                Log.i(TAG, "sendNotification: SALES INTENT");
            }
        }
    }

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FirebaseIdService.CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notifications_black_24dp)
            .setContentTitle(data != null ? data.get(ENotifElements.TITLE_KEY.value()) : "ERROR")
            .setContentText(data != null ? data.get(ENotifElements.TEXT_KEY.value()) : "ERROR")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setVibrate(new long[]{1000, 1000, 1000})
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Objects.requireNonNull(notificationManager).notify(new Random().nextInt(), notificationBuilder.build());
}

0 个答案:

没有答案