Question

I'm having the following two problems:

First Problem:

I have to register two customers who are using the same device, so they have the same Registration Id.

When I send an offer from the server side to both of these customers, the notification being displayed should depend on the logged in customer.

For example (see image):

enter image description here enter image description here In my application I have a customer login form. In this form, if user gnanavel logs in, the app has to receive his offers alone, and not the other's offers.

Similarly, if the user jeya logs in, the app has to receive her offers alone, and not the other's offers.

Currently the app is receiving both offers.

EDIT:

I have created login status field in my database.if my customer logs in the device means received the notification for his offers alone.i didn't get other's offers.its working well.

i have checked the condition while click the offer button:

 $getregid = mysqli_query($con,"select * from pim_customers where customer_id='$customer_id' and gcm_regid='$regId' and loginstatus='Y'");

i wrote these condition means my first problem is solved.but raised another problem.

The other problem is.,

When admin person enters some offer, the customer has to receive the notification on the device only when logged in the customer. Otherwise, the notification shouldn't be received. This part is working well.

The problem is, when my customer is logged out for a long time, and during that time the admin person entered more offers.

If the customer logs in the app after 2 days, he should receive all the pending messages. How can I get the pending messages?

admin side enters offers that time the customer logged out means loginstatus is 'N' in database.so how can execute these condition and receive the notification.

 $getregid = mysqli_query($con,"select * from pim_customers where customer_id='$customer_id' and gcm_regid='$regId' and loginstatus='Y'");

This is my server side code executed when clicking the offer button :

if (isset($_GET["regId"]) && isset($_GET["customer_id"]) && isset($_GET["message"])) {
  $regId = $_GET["regId"];
  $message = $_GET["message"];
  $customer_id = $_GET["customer_id"];
  include_once './GCM.php';
  $getregid = mysqli_query($con,"select * from pim_customers where customer_id='$customer_id' and gcm_regid='$regId' and loginstatus='Y'");
  $count1=mysqli_num_rows($getregid);
  while($row = mysqli_fetch_array($getregid))
  {
    $id=$row['customer_id'];
  }
  if (mysqli_num_rows($getregid) > 0) {
    $query = mysqli_query($con,"select count(offer) as count from pim_productdeal where customer_id='$id' and offer!=''");
    $count = mysqli_fetch_array($query);
    $msg = $count['count'];
    $gcm = new GCM();
    $registatoin_ids = array($regId);
    $message = array("price" => $msg." "."The offers received");
    $result = $gcm->send_notification($registatoin_ids, $customer_id, $message);
    echo $result;
  }
}

This is my Android side code:

public class GCMIntentService extends GCMBaseIntentService {

  private static final String TAG = "GCMIntentService";
  String regId;

  public GCMIntentService() {
    super(SENDER_ID);
  }

  @Override
  protected void onRegistered(Context context, String registrationId) {
    ServerUtilities.register(context, RegisterActivity.first_name, RegisterActivity.last_name, RegisterActivity.email, RegisterActivity.password, registrationId);
  }

  @Override
  protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
    displayMessage(context, getString(R.string.gcm_unregistered));
    ServerUtilities.unregister(context, registrationId);
  }

  @Override
  protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    regId = GCMRegistrar.getRegistrationId(this);
    String message = intent.getExtras().getString("price");
    displayMessage(context, message);
    if(Constants.response != null) {
      generateNotification(context,message);
    }
  }

  @Override
  protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    String message = getString(R.string.gcm_deleted, total);
    displayMessage(context, message);
    // notifies user
    generateNotification(context,message);
  }

  @Override
  public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
    displayMessage(context, getString(R.string.gcm_error, errorId));
  }

  @Override
  protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    displayMessage(context, getString(R.string.gcm_recoverable_error,
            errorId));
    return super.onRecoverableError(context, errorId);
  }

  private static void generateNotification(Context context,String message) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MyDealProducts.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      
  }
}
Was it helpful?

Solution

I edited your question to make it easier to understand. I hope that I didn't change your meaning. If I did, I apologize and ask you to correct me.

Now for my answer:

  1. Your server should be aware which user is currently logged in to which device. You should maintain in your server a table of logged in users, and for each logged in user it will hold the identifier of the device in which he/she is logged in. In order to do that you have to send a request to your server each time a user logs in or logs out. The request should contain the user id and the device id. For the device id you can either use the Registration ID or use some other ID that your server generates (it might be better to generate an ID in your server that would be shorter than the registration ID and therefore more efficient to use. You'll have to maintain a table that maps the registration ID to the device ID).

  2. When the admin creates a new offer and assigns it to a user, you will send a GCM notification to the app only if the user is logged in. Therefore, in your example of two users using the same device, if one of them is logged in, you will send only a single notification to that device for the logged in user. If none of them is logged in, you won't send any notifications. You will store the new offers in your DB and wait until one of them logs in.

  3. When a GCM message arrives to the device, you should make should that it is addressed to the current logged in user, since it's possible that one user logged out after the message was sent but another user logged in before it arrived to the device. For that purpose, your GCM message should include the user identifier as an additional field. If a message arrives to the device with a user ID that doesn't match the current logged in user, you discard that message and don't display a notification.

  4. When a user logs in, the server will receive a request (see #1). In the response of this request the server should return to the device all the offers that the user has in the server's DB.

  5. As for messages that were sent while the device was inactive for a long period of time - when the device becomes active again, old messages might be sent to it from GCM (if they haven't been discarded by GCM yet). If these messages don't belong to the current logged in user, you discard them (see #3). If they belong to the current logged in user, you should display them.

  6. I don't know PHP, but from my limited understanding of your server's code, it seems that you send to the device a GCM message that contains the number of offers the user has. You should probably use the same collapse_key for all messages you send. This way, if multiple messages were sent by your server while the device was offline, only one of them will be sent to the device when it becomes active again. You don't need to get all the pending messages if all they contain is the number of available offers.

OTHER TIPS

You don't have to set time_to_live when sending message from your server. According to the docs default time-to-live is 4 weeks

Also make sure you don't unregister the device on user logout.

When you send a push notification from the server add a Notification_id field to it. Now use local database (SQLite) in your mobile to manage the notification ids which have been displayed to the user.

You need to implement a call to your web server for getting the current list of all notifications when the app starts. So that the notifications ids from the server can be matched with the local notification ids. Then you can fetch the details for those particular notifications whose id is not in your database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top