php – Android GCM中的通知生成问题
作者:互联网
我遇到以下两个问题:
第一个问题:
我必须注册两个使用相同设备的客户,因此他们具有相同的注册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 one rror(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);
}
}
解决方法:
我编辑了您的问题,以便更容易理解.我希望我没有改变你的意思.如果我这样做,我道歉并请你纠正我.
现在我的回答:
>您的服务器应该知道哪个用户当前登录到哪个设备.您应该在服务器中维护一个已登录用户的表,并且对于每个登录用户,它将保存他/她登录的设备的标识符.为此,您必须向服务器发送请求每次用户登录或注销时.请求应包含用户标识和设备标识.对于设备ID,您可以使用注册ID或使用服务器生成的其他ID(最好在服务器中生成一个比注册ID短的ID,因此使用效率更高.你会必须维护一个将注册ID映射到设备ID的表.
>当管理员创建新要约并将其分配给用户时,只有在用户登录时才会向应用程序发送GCM通知.因此,在您使用同一设备的两个用户的示例中,如果其中一个是登录后,您将仅为登录用户向该设备发送一个通知.如果他们都没有登录,您将不会发送任何通知.您将在数据库中存储新商品,并等待其中一个商家登录.
>当GCM消息到达设备时,您应该将其发送给当前登录的用户,因为一个用户可能在消息发送后注销但另一个用户在到达设备之前登录.为此,您的GCM消息应包含用户标识符作为附加字段.如果消息到达设备的用户ID与当前登录的用户不匹配,则会丢弃该消息并且不显示通知.
>当用户登录时,服务器将收到请求(请参阅#1).在此请求的响应中,服务器应将用户在服务器的DB中提供的所有商品返回给设备.
>对于在设备长时间处于非活动状态时发送的消息 – 当设备再次变为活动状态时,可能会从GCM向其发送旧消息(如果它们尚未被GCM丢弃).如果这些消息不属于当前登录用户,则将其丢弃(请参阅#3).如果它们属于当前登录用户,则应显示它们.
>我不了解PHP,但是由于我对您的服务器代码的了解有限,似乎您向设备发送了一条GCM消息,其中包含用户拥有的优惠数量.您应该对发送的所有邮件使用相同的collapse_key.这样,如果服务器在设备脱机时发送了多条消息,则当它再次变为活动状态时,其中只有一条将被发送到设备.如果所有消息都包含可用商品的数量,则无需获取所有待处理消息.
标签:intentservice,android,php,push-notification,google-cloud-messaging 来源: https://codeday.me/bug/20190831/1776829.html