繁体   English   中英

我的应用未收到来自GCM的任何消息

[英]my app doesn't receive any messages from GCM

我的应用程序不会收到来自我创建的发件人页面和接收课程的gcm的任何消息,我只想在通知栏上显示它

GCM.php

       <?php

class GSM {

function _construct() {

}

    public function send_notification($registration_ids, $message){

        include_once './config.php';


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

        $fileds = array(
            'registration_ids' => $registration_ids,
            'message' => $message,

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

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

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

        $result = curl_exec($ch);
        if ($result === false) {

            die('Cutl failed:' . curl_error($ch));
        }
        curl_close($ch);
        echo $result;
    }
}



?>

index.php

    <!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){

            });
            function sendPushNotification(id){
                var data = $('form#'+id).serialize();
                $('form#'+id).unbind('submit');                
                $.ajax({
                    url: "send_message.php",
                    type: 'GET',
                    data: data,
                    beforeSend: function() {

                    },
                    success: function(data, textStatus, xhr) {
                          $('.txt_message').val("");
                    },
                    error: function(xhr, textStatus, errorThrown) {

                    }
                });
                return false;
            }
        </script>
        <style type="text/css">
            .container{
                width: 950px;
                margin: 0 auto;
                padding: 0;
            }
            h1{
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                font-size: 24px;
                color: #777;

            }
            div.clear{
                clear: both;
            }
            ul.devices{
                margin: 0;
                padding: 0;
            }
            ul.devices li{
                float: right;
                list-style: none;
                border: 1px solid #dedede;
                padding: 10px;
                margin: 0 15px 25px 0;
                border-radius: 3px;
                -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                -moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                color: #555;
            }
            ul.devices li label, ul.devices li span{
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                font-size: 12px;
                font-style: normal;
                font-variant: normal;
                font-weight: bold;
                color: #393939;
                display: block;
                float: right;
            }
            ul.devices li label{
                height: 25px;
                width: 50px;                
            }
            ul.devices li textarea{
                float: right;
                resize: none;
            }
            ul.devices li .send_btn{
                background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
                background: -webkit-linear-gradient(0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
                background: -moz-linear-gradient(center top, #0096FF, #005DFF);
                background: linear-gradient(#0096FF, #005DFF);
                text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
                border-radius: 3px;
                color: #fff;
            }
        </style>
      <style>

      </style>
    </head>
    <body>
        <?php
        include_once 'db_functions.php';
        $db = new DB_Functions();
        $users = $db->getAllUsers();
        if ($users != false)
            $no_of_users = mysql_num_rows($users);
        else
            $no_of_users = 0;
        ?>


        <div class="container" align="right">

            <h1><?php echo $no_of_users; ?> :عدد الأجهزة المسجلة </h1>
            <hr/>

            <ul class="devices">
                <?php
                if ($no_of_users > 0) {
                    ?>
                    <?php
                    while ($row = mysql_fetch_array($users)) {
                        ?>

                        <li>
                            <form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification)'
                            <?php echo $row["id"] ?>')">

                                <label dir="rtl">الإسم: </label> <span><?php echo $row["name"] ?></span>
                                <div class="clear"></div>
                                <label dir="rtl" >الإيميل:</label> <span><?php echo $row["email"] ?></span>
                                <div class="clear"></div>
                                <div class="send_container">                                
                                    <textarea dir="rtl" rows="3" name="message" cols="25" class="txt_message" placeholder="اكتب الرسالة هنا"></textarea>
                                    <input type="hidden" name="regId" value="<?php echo $row["gcmregid"] ?>"/>
                                    <input type="submit" class="send_btn" value="إرسال" onclick=""/>
                                </div>
                            </form>

                        </li>

                    <?php }
                } else { ?> 

                    <li>


                                    لايوجد أجهزة مسجلة

                    </li>

                <?php } ?>
            </ul>
        </div>

    </body>
</html>

GCMIntentService.java

    public class GCMIntentService extends GcmListenerService{


      private static final String TAG = "GCMIntentService";

    @Override
    public void onMessageReceived(String from, Bundle data) {

        String message = data.getString("msg");
        Log.d(TAG, "from:" + from);
        Log.d(TAG, "message:" + message);

    }
}

AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
    android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" />
........

<service
            android:name=".GCMIntentService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

    </application>

====================

您可以使用rest client postman例如postman或chrome的advance rest client邮寄rest client发送消息。

在此处输入图片说明

如果收到消息,请检查以上方法,这是服务器端故障。

并在您的android应用中进行更改

@Override
    public void onMessageReceived(String from, Bundle data) {

        String message = data.getString("message");  //changed it from msg to message
        Log.d(TAG, "message:" + message);

    }

将此代码放在GCMIntentservice class

private void sendNotification(String message) {
    Intent intent = new Intent(this,FullMapActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        int color = getResources().getColor(R.color.my_notif_color);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    /*PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent,
            PendingIntent.FLAG_ONE_SHOT);*/

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {



        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.headerlogo)
                .setContentTitle("hey..! your booking is confirmed")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    else
    {

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.headerlogo)
                .setContentTitle("hey..! your booking is confirmed")
                .setContentText(message)
                .setAutoCancel(true)

                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 , notificationBuilder.build());
    }
}`

并在onMessageReceived上调用它

sendNotification(message);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM