Android GCM中的通知生成问题

用户名

我遇到以下两个问题:

第一个问题:

我必须注册使用同一设备的两个客户,因此他们具有相同的注册ID。

当我从服务器端向这两个客户发送报价时,显示的通知应取决于登录的客户。

例如(见图):

在此处输入图片说明 在此处输入图片说明在我的应用程序中,我有一个客户登录表。以这种形式,如果用户gnanavel登录,则该应用程序必须仅接收他的报价,而不是其他人的报价。

同样,如果用户jeya登录,则该应用程序必须仅接收她的报价,而不是其他人的报价。

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);      
  }
}
Eran

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. 我不了解PHP,但是由于我对服务器代码的了解有限,因此您似乎将包含用户提供的报价的GCM消息发送到设备。您可能应该对所有发送的邮件使用相同的crash_key。这样,如果设备离线时服务器发送了多条消息,则当设备再次处于活动状态时,仅其中一条会发送到设备。如果它们包含的仅是可用报价的数量,则无需获取所有待处理的消息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android中的GCM与Socket

来自分类Dev

CrashLytics Android集成问题

来自分类Dev

Android-Google+集成问题“发生内部错误”

来自分类Dev

单击GCM通知未在Android Kitkat中打开所需的活动

来自分类Dev

GCM新的推送通知替换了Android中的旧通知数据

来自分类Dev

gnuplot密钥生成问题

来自分类Dev

Android Studio和NDK集成问题

来自分类Dev

Android Amazon SNS问题(GCM)

来自分类Dev

Java中的二维条码生成问题

来自分类Dev

从Android中的GCM推送通知获取当前上下文

来自分类Dev

Android中GCM的实现

来自分类Dev

使用GCM的Android推送通知是免费的

来自分类Dev

Android通知图标问题

来自分类Dev

Android Chrome完整浏览器apk生成问题

来自分类Dev

GCM-如何为Android推送通知生成服务器API密钥

来自分类Dev

Visual Basic生成问题

来自分类Dev

Android FCM通知问题

来自分类Dev

快速日期生成问题

来自分类Dev

离子生成问题

来自分类Dev

在Xamarin android中推送通知问题

来自分类Dev

Android Studio和NDK集成问题

来自分类Dev

引用中的Android Facebook集成问题

来自分类Dev

GCM推送通知在Android App中不起作用

来自分类Dev

PHP JSON生成问题

来自分类Dev

使用GCM和Django处理Android中的推送通知

来自分类Dev

Android FCM通知问题

来自分类Dev

Mac(Xamarin)上的VSTS生成代理中的生成问题

来自分类Dev

Android gcm通知打开片段

来自分类Dev

Lyft Android SDK 集成问题