-2

i'm trying to count a notification from OneSignal, and make it display in the badge, but i still getting this error. What's wrong with my code?

public class HomeActivity extends AppCompatActivity implements NetworkBroadcastListener.iNetStats, iNotif {

Context context;
SharedPref spref;
TextView notif_badge;

protected void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
        OneSignal.startInit(this).init();
        setContentView(R.layout.activity_home);

notif_badge = findViewById(R.id.cart_badge);
        setupBadge();



private void setupBadge() {
        int counter = spref.GetInt(localPref.TOTALNOTIF);
        context=this;
        spref = new SharedPref(this);
        notif_badge.setText(String.valueOf(Math.min(counter,99)));
    }

Here is my OneSignal ReceiveHandler

public class NotifReceivedHandler implements OneSignal.NotificationReceivedHandler {

    Context context;

    SharedPref spref;

    public NotifReceivedHandler(Context context){
        this.context = context;
        spref = new SharedPref(context);
    }

    @Override
    public void notificationReceived(OSNotification notification) {
        Intent intent = new Intent();
        intent.setAction("com.x.x.NOTIFICATION");
        intent.putExtra("body",notification.payload.body);
        intent.putExtra("title",notification.payload.title);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
        spref.AddInt(localPref.TOTALNOTIF,spref.GetInt(localPref.TOTALNOTIF)+1);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Fawwaz
  • 27
  • 1
  • 10

1 Answers1

0

Your SharedPref object is null, do like this way:

private void setupBadge() {
    spref = new SharedPref(this); // This line initializes spref object.
    int counter = spref.GetInt(localPref.TOTALNOTIF); // here, spref was null because, it wasn't initialized yet.
    context=this;
    notif_badge.setText(String.valueOf(Math.min(counter,99)));
}
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • hey thank you it's working, but when i opened the notification. it should be dissapeared or reduced, how to do this? i'm new with this. appreciated – Fawwaz Oct 13 '18 at 07:07
  • You'll need to decrease your counter from shared preference once your notification opened, also don't forget to update the UI. – Jeel Vankhede Oct 13 '18 at 07:10
  • spref.AddInt(localPref.DECNOTIF,spref.GetInt(localPref.TOTALNOTIF)-1); is this right? – Fawwaz Oct 13 '18 at 07:15
  • No, save your decremented counter on same variable like this : `spref.AddInt(localPref.TOTALNOTIF, spref.GetInt(localPref.TOTALNOTIF) - 1);` – Jeel Vankhede Oct 13 '18 at 07:19
  • So how i implement a method in my Activity? if the shared preference variable is same? – Fawwaz Oct 13 '18 at 07:26
  • It's unclear from the question, and there's no lot of code in it which makes me understand your requirement. consider asking another question or share some code with updated question about what you want to achieve. – Jeel Vankhede Oct 13 '18 at 07:29