ALSP
ALSP

Reputation: 102

The same push notification keeps appearing whenever i open my apps

The same push notification keeps appearing whenever I reopen my apps although i have already cleared the notification in the notification bar. Secondly how do I implement a service so that my apps can receive notification although the apps is closed.

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


@Override

public int onStartCommand(Intent intent, int flags, int startId) {

SharedPreferences sharedPreferences = getSharedPreferences(Constants.SHARED_PREF, MODE_PRIVATE);


String id = sharedPreferences.getString(Constants.UNIQUE_ID, null);


Firebase firebase = new Firebase(Constants.FIREBASE_APP + id);



firebase.addValueEventListener(new ValueEventListener() {


    @Override
    public void onDataChange(DataSnapshot snapshot) {

        String msg = snapshot.child("msg").getValue().toString();


        if (msg.equals("none"))
            return;

        showNotification(msg);
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        Log.e("The read failed: ", firebaseError.getMessage());
    }
});

return START_STICKY;
 }


private void showNotification(String msg){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(NotificationListener.this,ViewRecord.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Notifier");
builder.setContentText(msg);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder.setAutoCancel(true);
notificationManager.notify(1, builder.build());
}

my service code as below. and i call the service at onCreate function in the 1st activity..

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

}
 }

Upvotes: 0

Views: 517

Answers (2)

Amit Tumkur
Amit Tumkur

Reputation: 2835

Posting this as answer since code in comment wud make it look unstructured isServiceStarted

public class MainActivity extends AppCompatActivity {
private SharedPreferences servicePref;
private boolean isServiceStarted;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    servicePref = getSharedPreferences("servicePref", MODE_PRIVATE);
    isServiceStarted = servicePref.getBoolean("isServiceStarted", false);

    if (!isServiceStarted) {
        startService(new Intent(this, MyService.class));
        servicePref.edit().putBoolean("isServiceStarted",true).apply();
    }
}

and in ur MyService.class inside onStop method do this without fail.

public class MyService extends Service {
     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
         return null;
     }

     @Override
     public void onDestroy() {
         super.onDestroy();
         // save value as false when service gets destroyed so as to start again when u open the app
         getSharedPreferences("servicePref", MODE_PRIVATE).edit().putBoolean("isServiceStarted",false).apply();
     }
}

Upvotes: 1

Masum
Masum

Reputation: 4959

override the onStartCommand() method and then return START_STICKY.

Upvotes: 1

Related Questions