创建通知渠道(React Native)

huangapple go评论94阅读模式
英文:

Creating Notification Channel(React Native)

问题

package com.notifications;

import android.util.Log;
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 android.net.Uri;
import com.facebook.react.bridge.ReactApplicationContext;

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.setContentTitle("Notifiks");
    builder.setContentText("Narmal teksts");
    builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
    builder.setContentIntent(pendingIntent);

    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);
英文:

i'm building an notifications on my Android. Stuck on problem, where i need to create NotificationChannel.

getting error - Attempt to invoke virtual method 'java.lang.Object com.facebook.react.bridge.ReactApplicationContext.getSystemService(java.lang.Class)' on a null object reference

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";
}
}

Looks like the problem is with

  NotificationManager notificationManager = reactContext.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

答案1

得分: 2

你从未设置 reactContext 实例变量,这就是为什么它为 null。

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

英文:

You are never setting the reactContext instance variable, which is why it’s null.

In the constructor, you call super(reactContext) , but that wouldn’t set this class’s instance variable.

huangapple
  • 本文由 发表于 2020年3月16日 20:08:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定