Php Android GCM中的通知生成问题

Php Android GCM中的通知生成问题,php,android,push-notification,google-cloud-messaging,intentservice,Php,Android,Push Notification,Google Cloud Messaging,Intentservice,我有以下两个问题: 第一个问题: 我必须注册两个使用同一设备的客户,以便他们具有相同的注册Id 当我从服务器端向这两个客户发送报价时,显示的通知应取决于登录的客户 例如(见图): 在我的应用程序中,我有一个客户登录表单。在此表单中,如果用户gnanavel登录,则应用程序必须单独接收他的报价,而不是对方的报价 类似地,如果用户jeya登录,应用程序必须单独接收她的报价,而不是对方的报价 目前,该应用程序正在接收这两个服务 编辑: 我已经在数据库中创建了“登录状态”字段。如果我的客户登录设备,则

我有以下两个问题:

第一个问题:

我必须注册两个使用同一设备的客户,以便他们具有相同的注册Id

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

例如(见图):

在我的应用程序中,我有一个客户登录表单。在此表单中,如果用户
gnanavel
登录,则应用程序必须单独接收他的报价,而不是对方的报价

类似地,如果用户
jeya
登录,应用程序必须单独接收她的报价,而不是对方的报价

目前,该应用程序正在接收这两个服务

编辑:

我已经在数据库中创建了“登录状态”字段。如果我的客户登录设备,则意味着仅收到了他的报价通知。我没有收到其他人的报价。工作正常

我已在单击“报价”按钮时检查了条件:

 $getregid = mysqli_query($con,"select * from pim_customers where customer_id='$customer_id' and gcm_regid='$regId' and loginstatus='Y'");
我写下这些条件意味着我的第一个问题解决了,但提出了另一个问题

另一个问题是

当管理员输入一些报价时,客户只有在登录客户时才能在设备上收到通知。否则,不应收到通知。这部分工作正常

问题是,当我的客户注销很长一段时间,在此期间,管理员输入了更多的优惠

如果客户在2天后登录应用程序,他将收到所有待处理的消息。我如何才能获得挂起的消息

管理员端输入表示客户注销的时间意味着loginstatus在数据库中为“N”。那么如何执行这些条件并接收通知呢

 $getregid = mysqli_query($con,"select * from pim_customers where customer_id='$customer_id' and gcm_regid='$regId' and loginstatus='Y'");
这是单击“提供”按钮时执行的服务器端代码:

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;
  }
}
这是我的Android端代码:

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

从服务器发送消息时,不必将
时间设置为\u live
。根据
的规定,默认生存时间为4周


另外,请确保在用户注销时未注销设备。

当您从服务器发送推送通知时,请向其添加一个通知\u id字段。现在,在移动设备中使用本地数据库(SQLite)来管理已显示给用户的通知ID


您需要实现对web服务器的调用,以便在应用程序启动时获取所有通知的当前列表。因此,来自服务器的通知ID可以与本地通知ID匹配。然后,您可以获取那些id不在数据库中的特定通知的详细信息。

我编辑了您的问题,以便更容易理解。我希望我没有改变你的意思。如果我这样做了,我道歉并请你纠正我

现在让我回答:

  • 您的服务器应该知道哪个用户当前登录到哪个设备。您应该在服务器中维护一个登录用户表,对于每个登录用户,该表将保存他/她登录的设备的标识符。为了做到这一点,您必须在用户每次登录或注销时向服务器发送请求。请求应包含用户id和设备id。对于设备id,您可以使用注册id或使用服务器生成的其他id(最好在服务器中生成一个比注册ID短的ID,从而提高使用效率。您必须维护一个将注册ID映射到设备ID的表)

  • 当管理员创建新服务并将其分配给用户时,您将仅在用户登录时向应用程序发送GCM通知。因此,在您的两个用户使用同一设备的示例中,如果其中一个用户登录,您将仅向该已登录用户的设备发送一个通知。如果没有任何用户登录,您将不会发送任何通知。您将在数据库中存储新的优惠,并等待其中一个登录

  • 当GCM消息到达设备时,您应该确保它是发送给当前登录用户的,因为可能有一个用户在消息发送后注销,而另一个用户在消息到达设备之前登录。为此,您的GCM消息应该将用户标识符作为附加字段包含在内。如果到达设备的消息的用户ID与当前登录的用户不匹配,则放弃该消息,并且不显示通知

  •  $getregid = mysqli_query($con,"select * from pim_customers where customer_id='$customer_id' and gcm_regid='$regId' and loginstatus='Y'");
    
  • 当用户登录时,服务器将收到一个请求(请参见#1)。在响应此请求时,服务器应将用户在服务器数据库中的所有优惠返回给设备

  • 对于在设备长时间处于非活动状态时发送的消息,当设备再次变为活动状态时,可能会从GCM向其发送旧消息(如果GCM尚未丢弃旧消息)。如果这些消息不属于当前登录用户,则丢弃它们(请参阅#3)。如果它们属于当前登录用户,则应显示它们

  • 我不知道PHP,但我对服务器代码的理解有限,似乎您向设备发送了一条GCM消息,其中包含用户提供的服务数量。您可能应该对发送的所有消息使用相同的collapse_键。这样,如果在设备脱机时服务器发送了多条消息,则只有一条消息当设备再次激活时,其中的一条消息将被发送到设备。如果这些消息只包含可用的服务数量,则不需要获取所有挂起的消息


  • :这里我不提TTL。另外,如果我的客户注销意味着设备id仅可用。请查看我编辑的问题并给我答案。谢谢编辑并给出一个很好的答案。但我提出了另一个问题。@user2218667