Android推送通知服务无法在Lollipop上启动

时间:2015-05-26 10:02:14

标签: android push-notification broadcastreceiver android-5.0-lollipop

我在启动推送通知服务时遇到问题。

在Lollipop上,我的应用程序正在崩溃通知接收器的启动服务。

我的开始服务代码是。

Intent registrationIntent = new Intent(
                "com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app",
                PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        registrationIntent.putExtra("sender", "KEY");
        SplashScreen.this.startService(registrationIntent);

Menifest.xml代码

       <receiver
        android:name="com.example.notifications.C2DMReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >

        <!-- Receive the actual message -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="com.app.example" />
        </intent-filter>
        <!-- Receive the registration id -->
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.app.example" />
        </intent-filter>
    </receiver>

1 个答案:

答案 0 :(得分:3)

我在我的应用中遇到了同样的问题。

关注此link

我希望你能解决这个问题。

关于你的问题的一点解释。

在项目 lib 文件夹中下载并添加 gcm.jar 。 并在您要注册的java类中添加以下代码。

        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);

        // Get GCM registration id
        final String regId = GCMRegistrar.getRegistrationId(this);

        // Check if regid already presents
        if (regId.equals("")) {
            // Register with GCM
            GCMRegistrar.register(this, "SENDERKEY");
        } else {
            // Device is already registered on GCM Server
            if (GCMRegistrar.isRegisteredOnServer(this)) {
                String df = "dfd";
            } else {
                  GCMRegistrar.register(this, "SENDERKEY");
            }
        }

现在您在其中创建一个类 GCMIntentService.java 及其下面的代码。

 public class GCMIntentService extends GCMBaseIntentService {

 private static final String TAG = "GCMIntentService ";

 private static Context mContext;

 public GCMIntentService() {
    super("SENDERKEY");
 }

 @Override
 protected void onRegistered(Context context, String registrationId) {
     Debuger.e(TAG, "Device registered: regId = " + registrationId);
 }

@Override
protected void onUnregistered(Context context, String registrationId) {
     Debuger.e(TAG, "Device unregistered");
}

@Override
protected void onMessage(Context context, Intent intent) {
    Debuger.e(TAG, "Received message");

    mContext = context;
    String message = intent.getExtras().getString("message");
    if (message != null) {
        Debuger.e(TAG + "onMessage", "message = " + message);
    } else {
        message = "";
    }   
 }

@Override
protected void onDeletedMessages(Context context, int total) {
    // Debuger.e(TAG, "Received deleted messages notification");
}

@Override
public void onError(Context context, String errorId) {
    // Debuger.i(TAG, "Received error: " + errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    // Debuger.e(TAG, "Received recoverable error: " + errorId);
    return super.onRecoverableError(context, errorId);
}
}

AndroidMenifest.xml 文件中添加以下

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission     android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<permission
    android:name="com.example.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.example.gcm" />
        </intent-filter>
    </receiver>

    <service android:name="com.example.gcm.GCMIntentService" />