Search code examples
androidpush-notificationbroadcastreceiverandroid-5.0-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.

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

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

Solution

  • 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. And Add below code in your java class where you want to register.

            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.

     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

    <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" />