android - How to push firebase notification only when CheckBoxPreference is checked? -


i using firebase notifications in order notify users new data posted app. i'm following example given here: https://github.com/firebase/quickstart-android/tree/master/messaging

although, providing preference user. should notified when chose notified checking checkboxpreference.

i have set checkboxpreference , onpreferencechangelistener successfully. problem user getting notification when checkbox unchecked.

here's have done in settingsactivity:

public class settingsactivity extends preferenceactivity {      private appcompatdelegate mdelegate;      preference mypref;      boolean ischecked = true;      private static final string tag = "settingsactivity";      public static final string receive_notifs = "receivenotifications";      @override     protected void oncreate(bundle savedinstancestate) {         // todo auto-generated method stub         getdelegate().installviewfactory();         getdelegate().oncreate(savedinstancestate);         super.oncreate(savedinstancestate);         addpreferencesfromresource(r.xml.preferences);          getsupportactionbar().setdisplayhomeasupenabled(true);          mypref = (checkboxpreference) findpreference(receive_notifs);         mypref.setonpreferencechangelistener(new preference.onpreferencechangelistener() {             @override             public boolean onpreferencechange(preference preference, object o) {                 ischecked = boolean.valueof(o.tostring());                 if (ischecked) {                     toast.maketext(getbasecontext(), "checked", toast.length_short).show();                     if (getintent().getextras() != null) {                         string remotemessage = getintent().getextras().getstring("remotemessage");                         sendnotification(remotemessage);                     } else {                         toast.maketext(getbasecontext(), "error!", toast.length_short).show();                     }                 } else {                     toast.maketext(getbasecontext(), "unchecked", toast.length_short).show();                 }                 return true;             }         });      }      public void sendnotification(string messagebody) {         intent intent = new intent(this, settingsactivity.class);         intent.addflags(intent.flag_activity_clear_top);         pendingintent pendingintent = pendingintent.getactivity(this, 0 /* request code */, intent,                 pendingintent.flag_one_shot);          uri defaultsounduri= ringtonemanager.getdefaulturi(ringtonemanager.type_notification);         notificationcompat.builder notificationbuilder = new notificationcompat.builder(this)                 .setsmallicon(r.mipmap.ic_launcher)                 .setcontenttitle("fcm message")                 .setcontenttext(messagebody)                 .setautocancel(true)                 .setsound(defaultsounduri)                 .setcontentintent(pendingintent);          notificationmanager notificationmanager =                 (notificationmanager) getsystemservice(context.notification_service);          notificationmanager.notify(0 /* id of notification */, notificationbuilder.build());     } 

i have specified in above code notification should sent when checkboxpreference checked.

here's myfirebasemessagingservice.java file's code:

public class myfirebasemessagingservice extends firebasemessagingservice {      private static final string tag = "myfirebasemsgservice";      /**      * called when message received.      *      * @param remotemessage object representing message received firebase cloud messaging.      */     // [start receive_message]     @override     public void onmessagereceived(remotemessage remotemessage) {         // todo(developer): handle fcm messages here.         // if application in foreground handle both data , notification messages here.         // if intend on generating own notifications result of received fcm         // message, here should initiated. see sendnotification method below.         log.d(tag, "from: " + remotemessage.getfrom());         log.d(tag, "notification message body: " + remotemessage.getnotification().getbody());          intent intent = new intent(this, settingsactivity.class);         intent.putextra("remotemessage", remotemessage.getnotification().getbody());         startactivity(intent);     }     // [end receive_message]  } 

what doing here sending remotemessage intent settingsactivity , using in sendnotification() method there.

so, want want user notified when have chosen checking checkboxpreference.

please let me doing wrong here.

when use firebase notifications, message sent notification message. when app in foreground, code handling notification. when app backgrounded, default notification center handle it. , since notification center knows nothing of checkbox, display notification.

see answer issue on quickstart repo best explanation i've seen far: https://github.com/firebase/quickstart-android/issues/8#issuecomment-221039175

you have few options logic working:

  • don't send notification if user hasn't checked box. instead of suppressing display, don't send it. example, send topic users have opted in subscribe.

  • send data message instead of notification. ensures code invoked, when app backgrounded. see documentation on notifications , data in message payload more on difference between notifications , data messages.

related questions:


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -