2

I am working on an App where I want to show Push Notifications. Please note that since it is my client's requirement NOT to use any third party services, so using GCM/Firebase is out of question.

I am successfully able to show the notification from Service using the below code.

public class SendNotificationService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        CharSequence title = "Notification Title";
        CharSequence message = "This is a test notification.";

        Drawable drawable= ContextCompat.getDrawable(this,R.drawable.brand_icon_color);

        Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.brand_icon_small_color)
                .setLargeIcon(bitmap)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setOngoing(false);

        mBuilder.setContentText(message);
        mBuilder.setTicker(message);
        mBuilder.setWhen(System.currentTimeMillis());

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
        mBuilder.setContentIntent(pendingIntent);
        notificationManager.notify(0, mBuilder.build());

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
    }
}

I am starting this service through my AsyncTask onPostExecute method.

Intent intentService = new Intent(context, SendNotificationService.class);
context.startService(intentService);

I created this following a number of tutorials and found that if I will go to my Running Apps in Android Settings, I will be able to see this service running. But I was unable to locate any such service.

Now the problem is when I close my Application, the notification also disappears. I want it to stay until the user takes any action.

In addition to this, I want this service to start with phone startup even if the app is not started.

  • Check this out to run your service on boot: http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android – zed Mar 25 '17 at 16:45
  • "I want to show Push Notifications" -- nothing in your code has anything to do with this. "when I close my Application" -- please explain, **in detail**, what you mean by this. – CommonsWare Mar 25 '17 at 17:09
  • In the code I am able to show the Notification. How to Push this notification is not a challenge if I am able to run this Service in the background. What I mean from closing the App is when I simply slide the App from recent Apps or tap on X to close it, the notification disappears from the Notification Bar. – Mohammed Akhtar Zuberi Mar 25 '17 at 17:12
  • After implementing @zed solution, I am able to see the "run at startup" in App's permission. But there is nothing running on startup. To make it simpler, I am just displaying a Toast Message on onStartCommand now. – Mohammed Akhtar Zuberi Mar 25 '17 at 17:14
  • It isn't clear what you mean. You need to implement a push notification system. Your service has to connect to your server through a websocket, and your server can then start pushing messages to the clients (services) connected. – zed Mar 25 '17 at 17:18
  • Explaining it again. I want to show Push Notifications when there is a database change. How to do that is not a bigger worry for me. I am unable to show the notification when App is not running. To make it even simpler to explain, if I restart my phone with the App installed, it should show me the fix notification created in the above code. When I am running the App, it shows when called from within the App. – Mohammed Akhtar Zuberi Mar 25 '17 at 17:21
  • Your initial comment helped me in winning half the battle. I am able to call the service on Phone Startup. But the problem is same, why the service is not running? – Mohammed Akhtar Zuberi Mar 25 '17 at 17:23
  • @zed can you please post your comment as an answer so that I can accept it. It worked! Thank you! – Mohammed Akhtar Zuberi Mar 25 '17 at 18:18

1 Answers1

1

1) Add the permission to the manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2) Add a receiver to the manifest to run on boot:

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

In MyBroadcastReceiver.java:

package com.example;

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}
zed
  • 3,180
  • 3
  • 27
  • 38