GCM 推送通知设备未收到消息

Posted

技术标签:

【中文标题】GCM 推送通知设备未收到消息【英文标题】:GCM Push notification the device does not receiving messag 【发布时间】:2015-11-26 06:24:28 【问题描述】:

您好,我正在为我的应用程序使用gcm push notification。我将project id 发送到服务器并成功从服务器获取user id。服务器也成功发送了push message 我检查了它,但我的设备应用程序没有收到任何消息......这会是什么问题?谢谢

GCMIntentService.class

package com.yash.rastiyaujjalaapps;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMIntentService extends IntentService
    Context context;
    public  static final int NOTIFICATION_ID =1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";
    public int when=(int)System.currentTimeMillis();        
    public int per=when%100;
    private Handler handler;
     // System.out.println("...."+ when);

    public GCMIntentService() 
        super("237330017668");
        // TODO Auto-generated constructor stub
    

    @Override
    public void onCreate()
        super.onCreate();
        handler = new Handler();
    

    @Override
    protected void onHandleIntent(Intent intent) 
        // TODO Auto-generated method stub
        Bundle extras = intent.getExtras();
        String msg = intent.getStringExtra("message");
        System.out.println("...."+ msg);
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        String msg2 = extras.getString("message");
         if (!extras.isEmpty()) 

             if (GoogleCloudMessaging.
                        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) 
                    sendNotification("Send error: " + extras.toString());
                 else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_DELETED.equals(messageType)) 
                    sendNotification("Deleted messages on server: " +
                            extras.toString());
                // If it's a regular GCM message, do some work.
                 else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) 
                    System.out.println("...."+ msg);
                    // This loop represents the service doing some work.
                   for (int i=0; i<5; i++) 
                        Log.i(TAG, "Working... " + (i+1)
                                + "/5 @ " + SystemClock.elapsedRealtime());
                        try 
                            Thread.sleep(500);
                         catch (InterruptedException e) 
                        
                   
                    Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                    // Post notification of received message.
                    //sendNotification("Received: " + extras.toString());
                 String   msg1=msg2.trim();
                    sendNotification(msg1);
                    Log.i(TAG, "Received: " + extras.toString());
                
            
         GcmBroadcastReceiver.completeWakefulIntent(intent);
    
    private void sendNotification(String msg1) 
        //msg=msg.trim();
    long[] vibraPattern = 0, 500, 250, 500 ;
         Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


          // int when=(int)System.currentTimeMillis();        
        //int per=when%100;
         // System.out.println("...."+ when);
        mNotificationManager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);


     //   Intent notificationIntent = new Intent(context, MainActivity.class);
        //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            //  | Intent.FLAG_ACTIVITY_SINGLE_TOP);*/






   Intent myintent = new Intent(this, MainActivity.class);
   myintent.putExtra("message", msg1);
      //  myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
       .setAutoCancel(true)
        .setSmallIcon(R.drawable.logo)
        .setContentTitle("Rashtriya Ujala News")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg1))
        .setContentText(msg1)
        .setVibrate(vibraPattern)
        .setLights(0xff00ff00, 300, 100)
        .setSound(defaultSound)
        .setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
  //mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
       mNotificationManager.notify(per, mBuilder.build());






            // Update the TextView
           // TextView text = (TextView) findViewById(R.id.textView1);
           // text.setText("This app has been started " + counter + " times.");
            // Increment the counter
          


GCMBroadcastReceiver.class

package com.yash.rastiyaujjalaapps;

import android.app.Activity;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.WakefulBroadcastReceiver;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver

public void onReceive(Context mcontext, Intent intent) 
    // Explicitly specify that GcmIntentService will handle the intent.


        ComponentName comp = new ComponentName(mcontext.getPackageName(),
                GCMIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(mcontext, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);


    


清单文件是

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yash.rastiyaujjalaapps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="com.yash.rastiyaujjalaapps.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.yash.rastiyaujjalaapps.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name1"
    android:theme="@style/Theme.Sherlock.Light" >
     <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name="com.yash.rastiyaujjalaapps.SplashScreen"
        android:theme="@style/Theme.Sherlock.Light.NoActionBar" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>      

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

            <category android:name="com.yash.rastiyaujjalaapps" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMIntentService" />

    <activity
        android:name="com.yash.rastiyaujjalaapps.HomeMainScreen"
        android:theme="@style/Theme.Sherlock.Light.NoActionBar" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.MainActivity"
        android:icon="@drawable/mono"
        android:label="@string/app_name1" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.LiveVideo"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/rastiyaujjala"
        android:uiOptions="splitActionBarWhenNarrow" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.PoliticalMenu"
        android:label="@string/political"
        android:screenOrientation="landscape" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.AgricultureMenu"
        android:label="@string/agriculture"
        android:screenOrientation="landscape" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.SportMenu"
        android:label="@string/sport"
        android:screenOrientation="landscape" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.HomeDetails"
        android:icon="@drawable/mono" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Home"
        android:label="Home" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.National"
        android:label="@string/national" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.National2"
        android:icon="@drawable/mono"
        android:label="@string/national" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.International2"
        android:icon="@drawable/mono"
        android:label="@string/internatation" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Business2"
        android:icon="@drawable/mono"
        android:label="@string/business" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Rajya2"
        android:icon="@drawable/mono"
        android:label="@string/business" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Political2"
        android:icon="@drawable/mono"
        android:label="@string/political" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Technology2"
        android:icon="@drawable/mono"
        android:label="@string/technology" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Lifestyle2"
        android:icon="@drawable/mono"
        android:label="@string/lifestyle" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Rashifal2"
        android:icon="@drawable/mono"
        android:label="@string/rashifal" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.Sports2"
        android:icon="@drawable/mono"
        android:label="@string/sport" >
    </activity>
    <activity
        android:name="com.yash.rastiyaujjalaapps.VideoPageFragment"
        android:label="@string/sport"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    </activity>
    <!--
    <receiver android:name="com.personagraph.sensor.service.StartupReceiver"> <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED"/>
      <category android:name="android.intent.category.HOME"/> </intent-filter>
    </receiver>

    <service android:name="com.personagraph.sensor.service.SensorService"> 
       <intent-filter>
         <action android:name="com.personagraph.SensorService" /> 
       </intent-filter>
    </service>  


    -->
</application>

【问题讨论】:

检查通知是否真的发送到您的设备。通过在 GcmBroadcastReceiver 的 onReceive() 方法上放置一个虚拟的 Log,然后在 GCMIntentService.javaonHandleIntent() 方法上放置一个虚拟的 Log。检查它的断裂位置。 【参考方案1】:

在您的 AndroidManifest.xml 中,将 android:name=".GCMBroadcastReceiver" 替换为 android:name="com.yash.rastiyaujjalaapps.GCMBroadcastReceiver" 和 *service android:name=".GCMIntentService" * by service android:name="com.yash.rastiyaujjalaapps.GCMIntentService"

【讨论】:

现在用“com.yash.rastiyaujjalaapps.GcmBroadcastReceiver”替换“com.yash.rastiyaujjalaapps.GCMBroadcastReceiver” 是的,我认为这将是问题,但它仍然没有收到消息,我认为还有 1 个问题【参考方案2】:

在新版本的 google android play 服务与以前的版本有很大的变化。你可以参考google的this tutorial(一步一步)。它简单易懂。祝你好运!

【讨论】:

但此示例在我的另一个应用程序中有效,但在此应用程序中无效 对不起,我没有检查你的代码。我总是关注更新新的 API。在这个问题中,我不想调试您的代码,因为在新的 Api 中,发送通知和接收它们非常简单。为什么不引用新的 api? 嘿,你能告诉我在哪里可以找到这方面的教程吗?? 您可以在此处参考此 tut 解决方案:androidhive.info/2012/10/… 或使用解析:androidhive.info/2015/06/… 或使用新 api:developers.google.com/cloud-messaging/android/start

以上是关于GCM 推送通知设备未收到消息的主要内容,如果未能解决你的问题,请参考以下文章

某些 android 设备未收到 GCM 推送通知

未收到 Phonegap iOS 推送通知

某些设备在 android 中未收到推送通知

gcm推送通知:先成功,后IOS未注册

在android中没有收到推送通知

未收到来自 Amazon SNS 的 GCM 推送通知