0

how to get notification before open app or when i kill application still notification show on mobile device. or when i start phone i want notification but application should not running.

public class BeaconService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showNotification();
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
    private void showNotification() {
        NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_loc)
                        .setContentTitle("Welcome to Brillio")
                        .setContentText("Hello Mansur, Welcome to Brillio.")
                        .setPriority(2)
                        .setOnlyAlertOnce(false);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(2001, mBuilder.build());
    }
}

BeaconReceiver.java

public class BeaconReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intentLunch = new Intent(context, BeaconService.class);
        context.startService(intentLunch);

    }
}

menifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <service
        android:name=".BeaconService"/>
        <receiver android:name=".BeaconReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
Alam
  • 41
  • 8
  • @saeed notification display but want before open app. – Alam Mar 03 '16 at 04:01
  • @saeed My actual problem is I want notification show in background service means when I start my mobile then notification show automatically . – Alam Mar 03 '16 at 04:15
  • Can you Test show showNotification(); is calling or not .. by just Log – saeed Mar 03 '16 at 04:23
  • @saeed show showNotification(); calling but when i stop or kill app show notification not calling – Alam Mar 03 '16 at 04:25

1 Answers1

0

There is no straightforward way to start a service on just installing the app, without user opening it first time.

you just check these two links for more

1) How to start a Service when .apk is Installed for the first time

2)How to start android service on installation

To make presistant Notification, add this line:

builder.setOngoing(true)

Hope this will helps you

Community
  • 1
  • 1
saeed
  • 1,935
  • 1
  • 18
  • 33