开发者社区> 问答> 正文

创建通知频道(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);

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 16:00:28 567 0
1 条回答
写回答
取消 提交回答
  • 您永远不会设置reactContext实例变量,这就是为什么它为null的原因。

    在构造函数中,您调用super(reactContext),但这不会设置此类的实例变量。

    回答来源:Stack Overflow

    2020-03-22 16:00:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
利用编译将 Vue 组件转成 React 组件 立即下载
React Native 全量化实践 立即下载
React在大型后台管理项目中的工程实践 立即下载