Cordoba - Background Service Notice

I am using cordova to create an android application. Since the android service is killing, I associate the service with a notification to avoid killing the service.

Here is my method of how I associate a service with a notification

@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); } 

Here is the conclusion

enter image description here

A notification is generated, but the notification title is not the same as me. In addition, when I click this notification, it goes to the activity of the application information. But I want to move on to my main activity.

Has anyone encountered the same problem? Or does my code need any changes for the cordova?

+6
source share
2 answers

Thought about it after a long try. The problem was that my expected name is activity. Below code worked for me

 String package_name = this.getApplication().getPackageName(); Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(package_name); 
+3
source

when I click this notification, going to the activity of the application information. But I want to go to my main activity , to achieve this, change this line

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

to this line

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

Hope this helps you

+3
source

Source: https://habr.com/ru/post/1015512/


All Articles