Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM push notification is not showing in some devices when app is not running

I am implement GCM Push Notification in my app and its successfully done but in some devices it not show notification when app is close.

List of device which notication is not show:

Redmi-2

lenovo

Gionee

Can anyone explain me what is problem and how i solve its.

here my manifest:-

     <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="student.skoolstar.android.catalyst.com.schoolstar.skoolstarstudent">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />

  <permission
   android:name="student.skoolstar.android.catalyst.com.schoolstar.skoolstarstudent.permission.C2D_MESSAGE"
   android:protectionLevel="signature" />

  <uses-permission android:name="student.skoolstar.android.catalyst.com.schoolstar.skoolstarstudent.permission.C2D_MESSAGE" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.VIBRATE" />

  <application
   android:name="student.skoolstar.android.catalyst.com.schoolstar.skoolstarstudent.Controller"
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:supportsRtl="true"
   android:theme="@style/MyMaterialTheme">
   <activity android:name=".MainActivity">
    <intent-filter>
     <action android:name="android.intent.action.MAIN" />

     <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
   </activity>
   <activity
    android:name=".Login"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
   <activity
    android:name=".ListOfClass"
    android:screenOrientation="portrait">
   </activity>
   <activity
    android:name=".EditProfile"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
   <activity
    android:name=".ShowStudentList"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
   <receiver
    android:name="student.skoolstar.android.catalyst.com.schoolstar.skoolstarstudent.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="schoolstar.com.catalyst.android.skoolstar" />
    </intent-filter>
   </receiver>

   <service android:name=".GCMNotificationIntentService" />
   <activity
    android:name=".Message"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
   <activity
    android:name=".Attendance"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>

   <activity
    android:name=".NewMessage"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
   <activity
    android:name=".GroupMessage"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
   <activity
    android:name=".Test_Chat"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden">
   </activity>
  </application>

 </manifest>

here my service name GCMNotificationIntentService:-

    public class GCMNotificationIntentService extends GCMBaseIntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Database db;

    private Controller aController = null;

    public GCMNotificationIntentService() {
        // Call extended class Constructor GCMBaseIntentService
        super(Constants.GOOGLE_SENDER_ID);
    }

    public static final String TAG = "GCMNotificationIntentService";

    @Override
    protected void onRegistered(Context context, String registrationId) {

    }
    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.d("unref",registrationId);
        if(aController == null)
            aController = (Controller) getApplicationContext();
        Toast.makeText(getApplicationContext(),"hello no",Toast.LENGTH_LONG).show();

        aController.displayMessageOnScreen(context,
                getString(R.string.gcm_unregistered));
        aController.unregister(context, registrationId);
    }

    @Override
    public void onError(Context context, String errorId) {

        Log.d("error","");

        if(aController == null)
            aController = (Controller) getApplicationContext();

        aController.displayMessageOnScreen(context,

                getString(R.string.gcm_error, errorId));
    }
    @Override
    protected void onMessage(Context context, Intent intent) {

        if(aController == null)
            aController = (Controller) getApplicationContext();

        aController.acquireWakeLock(getApplicationContext());

        String message = intent.getExtras().getString("message");
        String formuser = intent.getExtras().getString("formuser");

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+5:30"));
        Date currentLocalTime = cal.getTime();
        DateFormat date = new SimpleDateFormat("HH:mm a");
        date.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
        String localTime = date.format(currentLocalTime);


        db = new Database(context);
        int  from_id = 0;

        List<FetchData> fetchdata = db.getAllContacts();
        for (FetchData fd : fetchdata)
        {

            from_id=fd.getID();//get ser no
        }

        db.storeMessage(420, formuser, from_id + "", message, "text", localTime, "F", "ST", "R");

        aController.displayMessageOnScreen(context, message);
        // notifies user
        sendNotification(context,message);
    }

    private void sendNotification(Context context,String msg) {
        String app_name = context.getResources().getString(R.string.app_name);
        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ListOfClass.class), 0);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(app_name)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText("New Message")
                .setSound(alarmSound);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        wl.acquire(15000);
        //  Log.d(TAG, "Notification sent successfully.");
    }
}

When I am see the whatsapp, hike and others notification app he will always run in background thread but my app is not running always in background. So may be this reason be also. I am recently working on android please help me.Thanks in advance!

like image 422
om_jaipur Avatar asked Feb 15 '16 05:02

om_jaipur


People also ask

How does GCM push notification work?

The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app.

Why are push notifications not working?

Settings > Sounds & Vibration > Do Not Disturb: if this setting is enabled, Push Notifications will not be received. Make sure this is disabled. Settings > General > Background App Refresh: this setting allows the app to run in the background and must be turned on.


3 Answers

I faced similar issue with Redmi-2. There is no problem in the code but it is due to Custom UI provided by the Manufacturer like MIUI 6. So to enable GCM notifications

Go to Security App >> Tap on permissions >> Tap on Auto Start and enable auto start for your App.

like image 152
Anup Dasari Avatar answered Oct 23 '22 12:10

Anup Dasari


There are two major reasons for that

1 - Some devices does not allow you to run a service on background like redmi-2 ( almost on all xiaomi device ). Even whats app could not work properly on them unless user allow them by going to Security App >> Tap on permissions >> Tap on Auto Start and enable auto start for whatsapp etc. In this case all you can do is to show the details of this to user on application start up. And open that screen and guide user ( if possible ) like clean master.

2- Second reason is that it didn't work on one of those phone whose Google Play Services app wasn't installed correctly(and its essential for GCM). You can not do anything on these devices also. The only thing you can do in this case is just show some message to user about this.

So from my experience there always be a number of users(but very small percentage) who wont receive GCM push.

like image 35
Nouman Ghaffar Avatar answered Oct 23 '22 11:10

Nouman Ghaffar


There is concept of whitelisting in Xiaomi's phone. So, if you put log in onReceive of gcm, you will notice that gcm is receiving but it's not processing further. Its cause your app is not whitelisted.

Xiaomi for security purposes disables the Notification for each Application. Follow these steps to receive messages in background once they quit the app using cleaner.

  • Enable Autostart
  • Enable Floating and Lock Screen Notification

Enable AutoStart

  • Open Security App.
  • Got to Permissions, then Click on Auto Start Management.
  • Add/Enable auto start apps (e.g. Whatsapp).

Enable Floating and Lock Screen Notification

  • Open Settings App.
  • Click on Notifications, then click on Manage Notifications.
  • Tap on the App which you are looking for (e.g. WhatsApp).
  • Enable Show in notification shade / Show on lockscreen and in drop down option.

For Reference check this out : http://support.hike.in/entries/55998480-I-m-not-getting-notification-on-my-Xiaomi-Phone-For-MIUI-6-

i got success with this.. Hope it helps..

like image 28
UMESH0492 Avatar answered Oct 23 '22 12:10

UMESH0492