Java Android版本8.1,startForeground的错误通知
我已经用了几天时间来解决这个问题。我的应用程序崩溃了,android版本是8.1。它在安卓8.0版中工作得非常好 我遵循下面提到的解决方案链接,并尝试了此链接中给出的许多解决方案,但我的应用程序尚未打开 我在FirebaseMessageService类中检查Android版本,并为Oreo版本创建频道,但下面每次都显示给定的错误Java Android版本8.1,startForeground的错误通知,java,android,firebase,push-notification,Java,Android,Firebase,Push Notification,我已经用了几天时间来解决这个问题。我的应用程序崩溃了,android版本是8.1。它在安卓8.0版中工作得非常好 我遵循下面提到的解决方案链接,并尝试了此链接中给出的许多解决方案,但我的应用程序尚未打开 我在FirebaseMessageService类中检查Android版本,并为Oreo版本创建频道,但下面每次都显示给定的错误 android.app.RemoteServiceException: Bad notification for startForeground: java.la
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
下面是我的代码。请帮我纠正这个问题
public class MyFirebaseMessagingService extends FirebaseMessagingService
{
private NotificationManager notifManager;
Context mContext;
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
sendNotification();
}
public void sendNotification()
{
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, m /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Logipace")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel(channelId, "LOGIPACE", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(m /* ID of notification */, notificationBuilder.build());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
下面是我在Activity类的onCreate()方法中使用的代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
// Create channel to show notifications.
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW));
}
请帮助我解决此问题。将您的逻辑放在收到的消息中。检查
onMessageReceived(RemoteMessage RemoteMessage)上的公共无效{
//处理FCM消息的数据有效负载。
Log.d(标记“From:”+remoteMessage.getFrom());
//检查消息是否包含数据有效负载。
如果(remoteMessage.getData().size()>0){
Log.d(标记,“消息数据有效负载:”+remoteMessage.getData());
Map params=remoteMessage.getData();
JSONObject对象=新的JSONObject(参数);
if(/*检查长时间运行的作业是否需要处理数据*/true){
//对于长时间运行的任务(10秒或更长时间),请使用Firebase作业调度器。
scheduleJob();
}否则{
//在10秒内处理消息
最终字符串msg=object.getString(“msg”);
发送通知(msg);
handleNow();
}
}
//检查消息是否包含通知负载。
if(remoteMessage.getNotification()!=null){
Log.d(标记,“消息通知正文:”+remoteMessage.getNotification().getBody());
}
}
私有void sendNotification(字符串messageBody){
意向意向=新意向(此,Home.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,意图,
悬挂式帐篷(一杆旗帜);
String channelId=getString(R.String.default\u notification\u channel\u id);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=
新建NotificationCompat.Builder(此,channelId)
.setSmallIcon(R.drawable.ic_stat_ic_通知)
.setContentTitle(getString(R.string.fcm_消息))
.setContentText(messageBody)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
//因为需要android Oreo通知通道。
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
NotificationChannel=新的NotificationChannel(信道ID,
“频道可读标题”,
NotificationManager.重要性(默认值);
notificationManager.createNotificationChannel(频道);
}
notificationManager.notify(通知的0/*ID*/,notificationBuilder.build());
}
您可以将其用于Oreo及更高版本
如果您使用服务类,则可以将此代码
public class BlablaService extends Service
@Override
public void onCreate(){
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_1)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
当然,不要忘记您的AndroidManifest.xml的ForeGroundService权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
我在检查版本时创建了自己的频道。代码与我看到的@Khaled LelaI在我的问题中提到的相同链接相同,我将引用此链接以获得解决方案。我将尝试模拟相同的情况以提供帮助。为什么要在onCreate
方法上设置逻辑,您必须在onMessageReceived
上设置,因为您为解决方案提供的链接也执行了相同的操作。检查该链接的第二个解决方案。这个人说把这个逻辑放在服务的onCreate()上。可能是重复的。我解决了我的问题,在我的位置跟踪类的某个地方使用了startForegroundService()方法。否则,上面的代码工作得很好。您能发送'R.string.default\u notification\u channel\u id'的值吗?
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />