6

I am using cordova to build my android application. Since android kills service, i am binding service with a notification to avoid service kill.

Here is my method how i bind the service with 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's the output

enter image description here

Notification is generated but notification title is not as i set. Also, when i click this notification, it's moving to app info activity. But i want to move to my main activity.

Does anyone faced this same issue? Or my code need any change for cordova?

Ijas Ahamed N
  • 5,632
  • 5
  • 31
  • 53
  • do you wanna Cordova notification app as a service in background? Is that your question? – Gandhi Mar 11 '17 at 10:08
  • @Gandhi I am starting a service from my plugin. And i don't want Android to kill it. So i an using foreground notification. I can notify it with notification but notification generated is not as my notification – Ijas Ahamed N Mar 11 '17 at 10:10
  • Shamed did you check out this plugin - https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-Background-Service – Gandhi Mar 11 '17 at 10:20
  • @Gandhi this plugin just start and stop a service. My plugin also can start and stop the service. Problem is with notifications – Ijas Ahamed N Mar 11 '17 at 10:24
  • I still dont understand your requirement. But just see if this link - http://stackoverflow.com/questions/41689316/initiating-a-phonegap-plugin-after-device-restart/41979127#41979127 helps – Gandhi Mar 11 '17 at 10:30
  • Any update on this? – Gandhi Mar 15 '17 at 07:33
  • @Gandhi Figured it out. Problem was with my pending intent. – Ijas Ahamed N Mar 15 '17 at 12:00
  • Ideally this has nothing to do with cordova framework then. I guess then it should not have been tagged under cordova – Gandhi Mar 15 '17 at 13:18
  • @Gandhi This was my first cordova project. I tried my previous code in an android studio project. It was working fine. So i thought it was with cordova. Now i have untagged cordova. Thanks. – Ijas Ahamed N Mar 16 '17 at 02:50
  • Thanks for the info. – Gandhi Mar 16 '17 at 04:45

2 Answers2

3

when i click this notification, it's moving to app info activity. But i want to move 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 it helps you

Naresh Kumar
  • 938
  • 5
  • 12
3

Figured it out after a long try. Problem was with my pending intent activity name. Below code worked for me

String package_name = this.getApplication().getPackageName();
Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(package_name);
Ijas Ahamed N
  • 5,632
  • 5
  • 31
  • 53
  • What was wrong with accepting the answer from [Naresh Kumar](http://stackoverflow.com/a/42759500/984830) which gave you this solution? – Nick Cardoso Mar 15 '17 at 17:21
  • @NickCardoso Sorry for not specifying whats wrong with my pending Intent. NotificationIntent i passed to pendingIntent was incorrect. It should be Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(package_name); – Ijas Ahamed N Mar 16 '17 at 02:47
  • Good clarification, you should probably limit your answer jsut to the lines which changed, to make it clearer – Nick Cardoso Mar 16 '17 at 10:17