java - 创建通知通道(React Native)
问题描述
我正在我的 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);
解决方案
您永远不会设置 reactContext 实例变量,这就是它为空的原因。
在构造函数中,您调用super(reactContext)
,但这不会设置此类的实例变量。
推荐阅读
- ios - 无法在 Swift 中调整多个设备上的布局
- javascript - 给定盒子的尺寸,为盒子包装的正方形找到最大可能的比例因子
- datastage - Datastage中基于场景的问题
- python - why i cannot use chain expression in numpy?
- azure - 在 Powershell 中为 Azure 规模集使用密码
- javascript - eval 得到 Uncaught SyntaxError: Unexpected identifier
- javascript - 将所有事件从一个对象重定向到另一个对象
- vue.js - Nuxt 中间件不适用于 vue 登录
- r - 如何在 ggplot 中对分布进行分组和拆分 [例如破坏 geom_line()]
- angular - Angular 6 primeng p-dataView 分页器仅生成第 1 页