我正在Android装置上建立通知。卡在问题上,我需要在其中创建NotificationChannel。
收到错误-尝试在空对象引用上调用虚拟方法'java.lang.Object com.facebook.react.bridge.ReactApplicationContext.getSystemService(java.lang.Class)'
NotificationService.java
package com.notifications;
import android.util.Log;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.Notification;
import android.app.PendingIntent;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.NotificationCompat.Builder;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.ReactPackage;
import android.net.Uri;
import android.app.NotificationManager.Policy;
public class NotificationService extends ReactContextBaseJavaModule {
private ReactApplicationContext reactContext;
public NotificationService(ReactApplicationContext reactContext) {
super(reactContext);
Log.i("NotificationService", "NotificationService Constructor");
createNotificationChannel();
displayNotification();
}
private void displayNotification() {
Log.i("NotificationService", "NotificationService test");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "textMessage");
intent.setType("text/plain");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Uri uri = intent.getData();
Log.i("NotificationService", "URI "+uri.toString());
PendingIntent pendingIntent = PendingIntent.getActivity(this.reactContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
String CHANNEL_ID = "notificationsChannel";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.reactContext, CHANNEL_ID);
// builder.setSmallIcon(17301575);
builder.setContentTitle("Notifiks");
builder.setContentText("Narmal teksts");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setContentIntent(pendingIntent);
// builder.setAutoCancel(false);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this.reactContext);
notificationManager.notify(123, builder.build());
}
private void createNotificationChannel() {
String CHANNEL_ID = "notificationsChannel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = new StringBuffer("charsequence");
String description = "kkads apraksts";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = reactContext.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
@Override
public String getName() {
return "NotificationService";
}
}
看起来问题出在
NotificationManager notificationManager = reactContext.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
问题来源:Stack Overflow
您永远不会设置reactContext实例变量,这就是为什么它为null的原因。
在构造函数中,您调用super(reactContext),但这不会设置此类的实例变量。
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。