我有这个服务可以连接到服务器并获取通知 但不幸的是它没有显示任何通知 这是服务类:
public class NotificationService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void startNotificationListener()
{
//start's a new thread
new Thread(new Runnable() {
@Override
public void run() {
//fetching notifications from server
//if there is notifications then call this method
ShowNotification();
}
}).start();
@Override
public void onCreate()
{
startNotificationListener();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
public void ShowNotification()
{
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(getBaseContext(),"notification_id")
.setSmallIcon(R.drawable.icon)
.setContentTitle("title")
.setContentText("content")
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.build();
notificationManager.notify(0, notification);
//the notification is not showing
}
}
调用 ShowNotification 时通知没有显示,我试过将 ShowNotification 的代码粘贴到主要 Activity 的 oncreate 中,这样
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(getBaseContext(),"n")
.setSmallIcon(R.drawable.icon)
.setContentTitle("Title")
.setContentText("Content Text"))
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.build();
notificationManager.notify(0, notification);
}
而且有效。
我是否总是需要从 Activity 创建通知? 如果没有,如何从服务创建通知?
P.S: the service will run even if the app is not running
最佳答案
是的,您可以从服务创建通知。但根据您的代码,startNotificationListener()
的右括号在下面的代码之后丢失了
new Thread(new Runnable() {
@Override
public void run() {
//fetching notifications from server
//if there is notifications then call this method
ShowNotification();
}
}).start();
并且您必须在 AndroidManifest.Xml
中注册服务</application>
之前的文件.
<service android:name=".NotificationService"/>
之后,您必须按照以下方式从您的 Activity 开始服务。
startService(new Intent(this,NotificationService.class));
有你的服务代码:
public class NotificationService extends Service {
public void startNotificationListener() {
//start's a new thread
new Thread(new Runnable() {
@Override
public void run() {
//fetching notifications from server
//if there is notifications then call this method
ShowNotification();
}
}).start();
}
@Override
public void onCreate()
{
startNotificationListener();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void ShowNotification()
{
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(getBaseContext(),"notification_id")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("content")
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.build();
notificationManager.notify(0, notification);
//the notification is not showing
}
您将收到通知。请查看屏幕截图
关于服务中未显示 android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999945/