成功:1在android中仍然没有通知 [英] Success:1 still no notification in android

查看:97
本文介绍了成功:1在android中仍然没有通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
multicast_id:

我试图通过GCM发送推送通知,但我得到: 4793018731783267982,
success:1,
failure:0,
canonical_ids:0,
results:[
{message_id: 0:1452928098906665%a69ccee8f9fd7ecd}
]
}

但仍然没有通知即将到来。这是我的代码:

  // index.php code 

$ app-> get( '/ notify',函数()使用($ app)
{
$ response = array();
$ regId =设备注册ID; //硬编码
$ message =hi everyone.this is to notify you;
$ registatoin_ids = array($ regId);
$ message = array(price=> $ message);
$ db = new DbHandler();
$ result = $ db-> send_notification($ registatoin_ids,$ message);
echoRespnse(200,$ result);

} );

// Dbhandler代码

公共函数send_notification($ registatoin_ids,$ message){


$ url ='https:/ /android.googleapis.com/gcm/send';

$ fields = array(
'registration_ids'=> $ registatoin_ids,
'data'=> $ message,
);

$ headers = array(
'Authorization:key = AIzaSyByR3xuTkhB-OZSNTlQqlwnqsLBzqXUWb0',// server key
'Content-Type:application / json'
);
$ ch = curl_init();

curl_setopt($ ch,CURLOPT_URL,$ url);

curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);

//临时禁用SSL证书支持
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ fields));

//执行后
$结果= curl_exec($ ch);
if($ result === FALSE){
die('Curl failed:'。curl_error($ ch));
}

//关闭连接
curl_close($ ch);
返回$ result;
}

注意:我直接从高级休息客户端运行这个API而不是android设备。我收到了成功,但没有通知设备的reg_id我硬编码here.Please帮助



编辑============ =====
我已经在android上写了这段代码:

  if(checkPlayServices())
{
gcm = GoogleCloudMessaging.getInstance(this);

if(regid.isEmpty())
{
new register()。execute();
}
else
{
Log.i(hello,没有找到有效的Google Play服务APK。);
}
}

public class register extends AsyncTask< Void,Integer,String> {

@Override
保护无效onPreExecute(){
super.onPreExecute();
}

@Override
保护字符串doInBackground(Void ... params){
String msg =;
尝试
{
if(gcm == null)
{
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
}
regid = gcm.register(AppConst.SENDER_ID);
Log.i(hello,当前设备的注册ID是:+ regid);
}
catch(IOException ex)
{
msg =Error:+ ex.getMessage();
}
返回null;
}

@Override
protected void onPostExecute(String result){
super.onPostExecute(result);


$ / code $ / pre
$ b $在Mainfest中我添加了:

 < receiver 
android:name =。GcmBroadcastReceiver
android:permission =com.google.android.c2dm .permission.SEND
>
< intent-filter>
< category android:name =com.example/>
< / intent-filter>

< / receiver>


解决方案

在Android设备上处理通知:


  @Override 
public void onMessageReceived(String from,Bundle data){
String message = data。的GetString( 消息);
Log.d(TAG,From:+ from);
Log.d(TAG,Message:+ message);
//在这里处理收到的消息。
}

欲了解更多信息,请访问: https://developers.google.com/cloud-messaging/


I am trying to send push notification through GCM but I am getting:

{
  "multicast_id":4793018731783267982,
  "success":1,
  "failure":0,
  "canonical_ids":0,
  "results": [
    { "message_id":"0:1452928098906665%a69ccee8f9fd7ecd" }
  ]
}

but still no notification is coming. Here is my code:

//index.php code

 $app->get( '/notify',function () use($app)
         {
                   $response = array ();
                    $regId="device registration id";   //hard coded
                    $message="hi everyone.this is to notify you" ;
                    $registatoin_ids = array($regId);
                    $message = array("price" => $message);
                    $db = new DbHandler ();
                    $result = $db->send_notification($registatoin_ids, $message);
                    echoRespnse ( 200, $result);

            } );

//Dbhandler code 

public function send_notification($registatoin_ids, $message) {


    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=AIzaSyByR3xuTkhB-OZSNTlQqlwnqsLBzqXUWb0',  //server key 
        'Content-Type: application/json'
    );
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    return $result;
}

Note:I am running this API directly from advanced rest client instead of android device.I am getting success in response but no notification on device whose reg_id i have hard coded here.Please help

Edit================= I have written this code on android end:

if (checkPlayServices()) 
{
    gcm = GoogleCloudMessaging.getInstance(this);

    if (regid.isEmpty())
    {
        new register().execute();
    }
    else
    {
        Log.i("hello", "No valid Google Play Services APK found.");
    }
}

public class register extends AsyncTask<Void, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... params) {
         String msg = "";
         try 
         {
              if (gcm == null) 
              {
                   gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
              }
              regid = gcm.register(AppConst.SENDER_ID);              
              Log.i("hello", "Current Device's Registration ID is: "+regid);     
         } 
         catch (IOException ex) 
         {
             msg = "Error :" + ex.getMessage();
         }
         return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

In Mainfest I have added:

     <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
         >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example" />
        </intent-filter>

    </receiver>

解决方案

Handle a notification on an Android device:

  @Override
 public void onMessageReceived(String from, Bundle data) {
   String message = data.getString("message");
   Log.d(TAG, "From: " + from);
   Log.d(TAG, "Message: " + message);
   // Handle received message here.
 }

For more info visit : https://developers.google.com/cloud-messaging/

这篇关于成功:1在android中仍然没有通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆