我正在尝试使用NotificationCompat为Android制作Notifications,但我遇到了一个问题:“错误:找不到符号”。 这是我的NotificationService.java文件。 我以为错误可能是由于导入引起的,但是我不确定,这个问题长期存在:(
package com.notifikacijas;
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;
public class NotificationService extends ReactContextBaseJavaModule {
private ReactApplicationContext reactContext;
public NotificationService(ReactApplicationContext reactContext) {
super(reactContext);
Log.i("NotificationService", "NotificationService Constructor");
this.reactContext = reactContext;
String CHANNEL_ID = "Kanaals";
NotificationCompat.Builder = new NotificationCompat.Builder(this.reactContext, CHANNEL_ID)
.setContentTitle("Virsraksts")
.setContentText("Teksts");
}
@Override
public String getName() {
return "NotificationService";
}
}
还有MainApplication.java
package com.notifikacijas;
import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.github.amarcruz.rnlog.RNLogPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
packages.add(new NotificationService());
return packages;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this); // Remove this line if you don't want Flipper enabled
}
/**
* Loads Flipper in React Native templates.
*
* @param context
*/
private static void initializeFlipper(Context context) {
if (BuildConfig.DEBUG) {
try {
/*
We use reflection here to pick up the class that initializes Flipper,
since Flipper library is not available in release mode
*/
Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
这是我的控制台错误输出。
/home/arturo/Notifikacijas/android/app/src/main/java/com/notifications/NotificationService.java:18: error: cannot find symbol
private ReactApplicationContext reactContext;
^
symbol: class ReactApplicationContext
location: class NotificationService
/home/arturo/Notifikacijas/android/app/src/main/java/com/notifications/NotificationService.java:20: error: cannot find symbol
public NotificationService(ReactApplicationContext reactContext) {
^
symbol: class ReactApplicationContext
location: class NotificationService
/home/arturo/Notifikacijas/android/app/src/main/java/com/notifications/NotificationService.java:25: error: cannot find symbol
NotificationCompat.Builder = new NotificationCompat.Builder(this.reactContext, CHANNEL_ID)
^
symbol: variable Builder
location: class NotificationCompat
/home/arturo/Notifikacijas/android/app/src/main/java/com/notifikacijas/MainApplication.java:28: error: constructor NotificationService in class NotificationService cannot be applied to given types;
packages.add(new NotificationService());
^
required: ReactApplicationContext
found: no arguments
reason: actual and formal argument lists differ in length
期待一些帮助,谢谢:) 反应本机:0.61.5
编辑:: 修复了该问题,现在在添加displayNotification()之后,它的构建没有错误,但是一直在我尝试启动它时说NotificationApp停止工作。
public void displayNotification() {
Intent intent = this.reactContext.getPackageManager().getLaunchIntentForPackage("lv.dtg");
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("Objektā atrodas darbinieki");
builder.setContentText("Vai vēlaties izreģistrēt darbiniekus no objekta?");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setContentIntent(pendingIntent);
// builder.setAutoCancel(false);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this.reactContext);
notificationManager.notify(123, builder.build());
}