我正在使用cordova來構建我的android應用程序。由於android殺死服務,我綁定服務與通知,以避免服務死亡。Cordova - 通知後臺運行服務
這裏是我的方法,我怎麼綁定服務,併發出通知
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this.getApplicationContext();
notifyService();
return START_NOT_STICKY;
}
private void notifyService() {
String package_name = this.getApplication().getPackageName();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name));
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Smart Home")
.setContentText("Smart Home running in background")
.setSmallIcon(this.getApplication().getResources().getIdentifier("icon", "drawable-hdpi", package_name))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(notificationId, notification);
}
這裏的生成
通知的輸出,但通知的標題是不是我設置。另外,當我點擊這個通知時,它正在移動到應用信息活動。但我想轉移到我的主要活動。
有沒有人遇到過這個問題?或者我的代碼需要更改cordova?
你想科爾多瓦通知應用程序作爲服務在後臺?這是你的問題嗎? – Gandhi
@甘地我從我的插件啓動一項服務。我不希望Android殺死它。所以我使用前臺通知。我可以通知它通知,但生成的通知不是我的通知 –
你看看這個插件是否感到羞恥 - https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-Background-Service – Gandhi