- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
显示错误:
Builder (Context) in Builder cannot be applied to (FirebaseMessagingService, java.lang.String)
帮我解决这个问题。
我 尝试过 Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID);
和
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
但它不适用于 API 23 和 API 27。
这是代码
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getData().get("title");
String notification_msg = remoteMessage.getData().get("body");
String from_user_id = remoteMessage.getData().get("from_user_id");
String click_action = remoteMessage.getData().get("click_action");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
Intent intent = new Intent(click_action);
intent.putExtra("user_id", from_user_id);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.logo1).setPriority(Notification.PRIORITY_MAX).setContentTitle(notification_title).setContentText(notification_msg).setContentInfo("Info").setContentIntent(pendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
notificationManager.notify(mNotificationId, notificationBuilder.build());
}
}
构建.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.android.gabwithus"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support:design:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.google.firebase:firebase-auth:16.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.1.0'
implementation 'com.android.support:support-v4:28.0.0-alpha3'
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
testImplementation 'junit:junit:4.12'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.firebaseui:firebase-ui-database:2.0.1'
implementation 'com.squareup.okhttp:okhttp:2.5.0'
implementation 'id.zelory:compressor:2.1.0'
implementation 'com.android.support:support-media-compat:28.0.0-alpha3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.5.1'
implementation 'com.facebook.fresco:fresco:1.5.0'
}
apply plugin: 'com.google.gms.google-services'
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '25.3.0'
}
}
}
}
最佳答案
尝试使用这个,我认为你的问题在这里:details.useVersion '25.3.0'
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '27.1.0'
}
}
}
}
关于android - 如何让它工作 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51256665/
我正在使用构建器类创建通知并使用 android 4.1 版本(API 级别 16)。但是应用程序正在生成异常08-28 12:39:00.321: E/AndroidRuntime(2311): j
玩了三个小时,不知道哪里出了问题。 private static Notification buildNotification(Context context) { NotificationC
这是我用来创建通知的代码。 notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.
我一直在使用 Notification.Builder 类,但有些选项显然已弃用,例如 SetSound、SetDefaults、SetVibrate、 SetPriority等 我现在应该使用什么方
我喜欢 NotificationBuilder 的易用性,需要经常更新我的通知。有什么方法可以让我在早期版本的 Android 上使用这个很棒的类? 最佳答案 Android 支持包包含 Notifi
我正在使用 v13 支持库。我使用通知生成器和 .setProgress 构建进度通知。它在 4.2.2 设备上运行良好,但在 2.3.5 上什么也没有显示。 这是正常行为吗? mNotifyMana
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_C
我是一名优秀的程序员,十分优秀!