Search code examples
androidandroid-studioparse-platformpush-notificationgenymotion

Parse Push notification works in Genymotion, but doesn't work in real phone


I've developed an app which uses parse push notification service. Through the entire process I've tested the app on GenyMotion while using it with android studio.

The parse push works fine there.

But Now after development when I am trying the same app in my Micromax Canvas A116 HD running Android 4.1.2 I simply donot get any push notification.

I have even set a BP in the broadcast receiver, it didn't break on it even once.

How ever the same app running in genyMotion gets the notification.

Here are the codes from the app :

Android Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pchakraverti.enotice" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:protectionLevel="signature"
        android:name="com.example.pchakraverti.enotice.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.example.pchakraverti.enotice.permission.C2D_MESSAGE" />

    <application
        android:name="com.example.pchakraverti.enotice.InitParse"
        android:hardwareAccelerated="false"
        android:allowBackup="true"
        android:icon="@drawable/e_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".clgIdVerificationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".StudentDetailsActivity"
            android:label="@string/title_activity_student_details"
            android:parentActivityName=".clgIdVerificationActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.pchakraverti.enotice.clgIdVerificationActivity" />
        </activity>
        <activity
            android:name=".NotificationActivity"
            android:label="@string/title_activity_notification"
            android:parentActivityName=".StudentDetailsActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.pchakraverti.enotice.StudentDetailsActivity" />
        </activity>
        <activity
            android:name=".WebViewActivity"
            android:label="@string/title_activity_web_view" >
        </activity>


        <service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

        <receiver android:name="com.parse.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" />

                <!--
                  IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
                -->
                <category android:name="com.example.pchakraverti.enotice" />
            </intent-filter>
        </receiver>

        <receiver android:name="com.example.pchakraverti.enotice.MyBroadcastReceiver" android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>


    </application>

</manifest>

InitParse:

package com.example.pchakraverti.enotice;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.SaveCallback;

/**
 * Created by PChakraverti on 6/3/2015.
 */
public class InitParse extends Application {
    @Override
    public void onCreate(){
        super.onCreate();
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "KEY", "KEY");
        Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
        /*ParsePush.subscribeInBackground("", new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
                } else {
                    Log.e("com.parse.push", "failed to subscribe for push", e);
                }
            }
        });*/
        ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.d("com.parse.push", "Ready for push.");
                } else {
                    e.printStackTrace();

                }
            }
        });
    }
}

MyBroadcastReceiver :

package com.example.pchakraverti.enotice;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.parse.ParsePushBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

import Database.Database;
import Database.NoticeInformation;

/**
 * Created by PChakraverti on 6/3/2015.
 */
public class MyBroadcastReceiver extends ParsePushBroadcastReceiver {
    @Override
    public void onPushReceive(Context context, Intent intent) {

        JSONObject json = null;

        Log.i("TAG", "Push Received");
        String data="", channel, action, f_name = "", n_id  = "0", n_type = "", n_head = "", n_file = "";
        try{
            action = intent.getAction();
            channel = intent.getExtras().getString("com.parse.Channel");
            data = intent.getExtras().getString("com.parse.Data");
            json = new JSONObject(data);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }

        try {
            n_type = json.getString("N_TYPE");
            n_head = json.getString("N_HEADER");
            n_file = json.getString("N_FILE");
        }catch (JSONException e){
            e.printStackTrace();
        }


        //Save Information to Database
        NoticeInformation ninfo = new NoticeInformation(0, n_type, n_head, +n_file, 0, 0);
        Database db = new Database(context);
        long r_id = db.insertIntoNoticeStorage(ninfo);

        Intent launchIntent = new Intent(context, WebViewActivity.class);
        launchIntent.putExtra("Link", n_file);
        launchIntent.putExtra("N_ID", String.valueOf(r_id));
        PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(), launchIntent, PendingIntent.FLAG_CANCEL_CURRENT);


        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.e_icon);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("Push Notification")
                .setContentText("A New Notice Was Published.")
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setLargeIcon(icon)
                .setSmallIcon(R.drawable.e_icon)
                .build();

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(1, notification);
    }
}

I am simply not able to understand what is going on and how to solve this. Does this have something to do with android version. I do not any device with latest android, but on Genymotion, the app works (push is received) in Android 4.1.1 .

Need some help with this!


Solution

  • I've found the solution to the problem. I've tested this in android 4.1.2, 4.3.

    The working class is given as follows :

    package com.pchakraverti.enotice_new;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.NotificationCompat;
    import android.util.Log;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.Random;
    
    import database.*;
    
    /**
     * Created by PChakraverti on 6/6/2015.
     */
    
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
    
        JSONObject json = null;
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            Log.i("TAG", "Push Received");
            String data="", channel, action, n_id  = "0", n_type = "", n_head = "", n_file = "";;
            try{
                action = intent.getAction();
                channel = intent.getExtras().getString("com.parse.Channel");
                data = intent.getExtras().getString("com.parse.Data");
                json = new JSONObject(data);
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
    
            try {
                n_type = json.getString("N_TYPE");
                n_head = json.getString("N_HEADER");
                n_file = json.getString("N_FILE");
            }catch (JSONException e){
                e.printStackTrace();
            }
    
            NoticeInformation ninfo = new NoticeInformation(0, n_type, n_head, n_file, 0, 0);
            Database db = new Database(context);
            long r_id = db.insertIntoNoticeStorage(ninfo);
    
            Intent launchIntent = new Intent(context, WebViewActivity.class);
            launchIntent.putExtra("Link", n_file);
            launchIntent.putExtra("N_ID", String.valueOf(r_id));
            PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(), launchIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    
            Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.e_icon);
    
            Notification notification = new NotificationCompat.Builder(context)
                    .setContentTitle("A notice has been published!")
                    .setContentText(n_head)
                    .setContentIntent(pi)
                    .setAutoCancel(true)
                    .setLargeIcon(icon)
                    .setSmallIcon(R.drawable.e_icon)
                    .build();
    
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.defaults |= Notification.DEFAULT_LIGHTS;
    
            NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(1, notification);
        }
    }
    

    The ParseBroacastReceiver simply fails. This how ever works perfectly.