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

问题描述 投票:1回答:1

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

在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);

Manifest.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>
android push-notification broadcastreceiver android-5.0-lollipop
1个回答
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);
}
}

并在您的AndroidManifest.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" />
© www.soinside.com 2019 - 2024. All rights reserved.