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!