如何从OneSignal发出徽章通知?

时间:2018-10-14 06:16:01

标签: android onesignal

我只是使用OneSignal制作了一个徽章通知计数器,我将一些收到的通知保存在我的localPref类上。当我做一个计数器来重置收到的通知的数量时,它正在工作,并且值变为0。但是当我关闭应用程序并再次打开它时,通知的数量仍然存在,并且应该为0值。如何获得?

这是我的MainActivity:

public class HomeActivity extends AppCompatActivity  {
Context context;
SharedPref spref;
TextView notif_badge;
ImageView logo;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    OneSignal.startInit(this).init().              
    setContentView(R.layout.activity_home);
    parent = findViewById(R.id.activity_home);


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

    logo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            resetCounter();
        }
    });

}



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

private void resetCounter(){
    spref = new SharedPref(this);
    int resCount = spref.GetInt(localPref.DECNOTIF);
    context=this;
    notif_badge.setText(String.valueOf(Math.min(resCount,0)));
}

这是一个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_andro.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);
    spref.AddInt(localPref.DECNOTIF,spref.GetInt(localPref.TOTALNOTIF)-spref.GetInt(localPref.TOTALNOTIF));


}

}

本地偏好类:

public class localPref {

    public static final String TOTALNOTIF="lp16";
    public static final String DECNOTIF="lp17";

}

0 个答案:

没有答案
相关问题