简体   繁体   English

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

[英]Android Push Notification service not start on Lollipop

I am facing an issue while start service for push notifications. 我在启动推送通知服务时遇到问题。

On Lollipop my app is crashing on start service for notifications receiver. 在Lollipop上,我的应用程序崩溃了通知接收器的启动服务。

My start service code is. 我的开始服务代码是。

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 Code 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>

I faced same problem in my app. 我的应用程序遇到了同样的问题。

Follow this link 点击此链接

I hope you will solve this problem. 我希望你能解决这个问题。

And little bit explanation regarding your issue. 关于你的问题的一点解释。

Download and add gcm.jar in your project lib folder. 在项目lib文件夹中下载并添加gcm.jar And Add below code in your java class where you want to register. 并在您要注册的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");
            }
        }

Now you create a class GCMIntentService.java and below code in it. 现在,您将在其中创建一个类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);
}
}

And in your AndroidMenifest.xml file add below 并在您的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" />

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

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