Java 未创建我的通知频道
每当我通过fcm发送通知时 它说它将使用默认的从清单或安卓应用程序,我也提到了我的频道清单作为默认。 它只是说频道不是由应用程序创建的 日志说Java 未创建我的通知频道,java,android,firebase,kotlin,firebase-cloud-messaging,Java,Android,Firebase,Kotlin,Firebase Cloud Messaging,每当我通过fcm发送通知时 它说它将使用默认的从清单或安卓应用程序,我也提到了我的频道清单作为默认。 它只是说频道不是由应用程序创建的 日志说 W/FirebaseMessaging: Notification Channel requested (message_notification) has not been created by the app. Manifest configuration, or default, value will be used. 这是我的onMessage
W/FirebaseMessaging: Notification Channel requested (message_notification) has not been created by the app. Manifest configuration, or default, value will be used.
这是我的onMessageReceived函数
override fun onMessageReceived(p0: RemoteMessage) {
super.onMessageReceived(p0)
sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+":/ /"+applicationContext.packageName+"/"+R.raw.alert)
Log.d("notofiction Nul", sound.toString())
if(p0.notification!=null){
val title = p0.data["title"]
val message = p0.data["body"]
Log.d("notottg",title+message)
buildNotificationChannel()
if(buildNotificationChannel()){
generateNotification(title!!,message!!)
}
}
else{
val title = p0.data["title"]
val message = p0.data["body"]
Log.d("notottg",title+message)
buildNotificationChannel()
if(buildNotificationChannel()){
generateNotification(title!!,message!!)
}
}
}
这是我的createNotificationChannel函数
private fun buildNotificationChannel():Boolean {
/*var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
Log.d("notisound",sound.toString())*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val CHANNEL_ID = "merchant_notification"
val CHANNEL_NAME = "Merchant Notification"
var notificationChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
vibrationPattern=longArrayOf(400, 400, 400, 400, 500, 400, 400, 400, 400)
enableVibration(true)
}
notificationManager= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
//notificationChannel.setSound(sound,audioAttributes)
}
return true
}
这是我的生成功能
fun generateNotification(title: String, message: String) {
val intent = Intent(this@CustomMessagingService, MainActActivity::class.java)
var pendingIntent = PendingIntent.getActivity(this@CustomMessagingService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
var builder: NotificationCompat.Builder =
NotificationCompat.Builder(this, "merchant_notification")
var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
Log.d("notisound",sound.toString())
builder.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setSound(sound)
notification = builder.build()
Log.d("notot", notification.toString() + " " + sound.toString())
notificationManager.notify(1, notification)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
var nb: Notification.Builder = Notification.Builder(this)
nb.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(sound, audioAttributes)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.build()
notification = nb.notification
notification.flags = Notification.FLAG_AUTO_CANCEL
notificationManager.notify(0, notification)
}
}
fun generateNotification(标题:String,消息:String){
val intent=intent(this@CustomMessagingService,MainActActivity::class.java)
var pendingent=pendingent.getActivity(this@CustomMessagingService,0,意图,挂起内容。标志(更新当前)
变量生成器:NotificationCompat.builder=
通知合作建造商(本“商户通知”)
var audioAttributes=audioAttributes.Builder().setContentType(audioAttributes.CONTENT\u TYPE\u SONIFICATION)
.setUsage(AudioAttributes.USAGE\u报警)
.build()
sound=Uri.parse(ContentResolver.SCHEME\u ANDROID\u RESOURCE+“://”+applicationContext.packageName+“/”+R.raw.alert)
Log.d(“notisound”,sound.toString())
builder.setAutoCancel(真)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(标题)
.setContentText(消息)
.setContentIntent(挂起内容)
.setSound(声音)
notification=builder.build()
Log.d(“notot”,notification.toString()+“”+sound.toString())
notificationManager.notify(1,通知)
if(Build.VERSION.SDK\u INT
我的舱单代码
<service android:name=".classes.CustomMessagingService"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"
/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="merchant_notification" />
这对我很有用:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // channel for notifications
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
List<NotificationChannel> channelList = notificationManager.getNotificationChannels();
for (int i = 0; channelList != null && i < channelList.size(); i++)
notificationManager.deleteNotificationChannel(channelList.get(i).getId());
}
String name = getString(R.string.notification_channel_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(getString(R.string.notification_channel_id), name, importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
Intent intent;
intent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setSmallIcon(R.drawable.logo)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentTitle(message.getData ().get ("myBody"))
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());
}
@Override
public void onNewToken(@NonNull String token) {
}
}
只是想一想——当您已经调用通知生成函数时,可能找不到making cahnnel函数。您可以尝试将它们放在同一个函数中,以确保它们正确发生,一个接一个地完成。还有,发布你的清单我照你说的做了。仍然不是信用渠道。你能发布你的清单吗?上传了清单,包括你给@Avital抱歉,我没有更多的想法。。。我使用Java,因此无法运行您的代码,但在我看来它很好:(我上传了我的
onreceive
,效果很好,也许你可以比较一下你的代码并找出错误。顺便说一句,创建频道只针对Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.O
,所以这个检查应该在metohd的开始,应该包括整个代码。)
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/notification_channel_id" />