应用未运行时,某些设备中未显示GCM推送通知 [英] GCM push notification is not showing in some devices when app is not running

查看:63
本文介绍了应用未运行时,某些设备中未显示GCM推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中实现GCM推送通知,并且成功完成了该通知,但是在某些设备中,当应用程序关闭时,它不显示通知.

未显示通知的设备列表:

Redmi-2

联想

Gionee

任何人都可以向我解释什么是问题以及我如何解决它.

这是我的清单:-

 <?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"/><权限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"/><应用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"><意图过滤器>< action android:name ="android.intent.action.MAIN"/>< category android:name ="android.intent.category.LAUNCHER"/></intent-filter></activity><活动android:name =.登录"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><活动android:name =.ListOfClass"android:screenOrientation ="portrait"></activity><活动android:name =.EditProfile"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><活动android:name =.ShowStudentList"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><接收器android:name ="student.skoolstar.android.catalyst.com.schoolstar.skoolstarstudent.GcmBroadcastReceiver"android:permission ="com.google.android.c2dm.permission.SEND"><意图过滤器><!-接收实际的消息.->< action android:name ="com.google.android.c2dm.intent.RECEIVE"/><!-接收注册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"/><活动android:name =.Message"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><活动android:name =.出勤"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><活动android:name =.NewMessage"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><活动android:name =.GroupMessage"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity><活动android:name =.Test_Chat"android:screenOrientation ="portrait"android:windowSoftInputMode ="stateHidden"></activity></应用程序></manifest> 

这里我的服务名称是GCMNotificationIntentService:-

 公共类GCMNotificationIntentService扩展了GCMBaseIntentService {public static final int NOTIFICATION_ID = 1;私有NotificationManager mNotificationManager;NotificationCompat.Builder构建器;数据库db;私有控制器aController = null;公共GCMNotificationIntentService(){//调用扩展类的构造方法GCMBaseIntentService超级(Constants.GOOGLE_SENDER_ID);}public static final String TAG ="GCMNotificationIntentService";@Override受保护的void onRegistered(Context context,String registrationId){}@Override受保护的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公共无效的onError(Context context,String errorId){Log.d(错误",");if(aController == null)aController =(Controller)getApplicationContext();aController.displayMessageOnScreen(context,getString(R.string.gcm_error,errorId));}@Overrideprotected void onMessage(Context context,Intent intent){if(aController == null)aController =(Controller)getApplicationContext();aController.acquireWakeLock(getApplicationContext());字符串消息= intent.getExtras().getString("message");字符串formuser = intent.getExtras().getString("formuser");日历cal = Calendar.getInstance(TimeZone.getTimeZone("GMT + 5:30"));;日期currentLocalTime = cal.getTime();DateFormat date = new SimpleDateFormat("HH:mm a");date.setTimeZone(TimeZone.getTimeZone("GMT + 5:30"));字符串localTime = date.format(currentLocalTime);db =新数据库(上下文);int from_id = 0;List< FetchData>fetchdata = db.getAllContacts();对于(FetchData fd:fetchdata){from_id = fd.getID();//获取序列号}db.storeMessage(420,formuser,from_id +",消息,文本",localTime,"F","ST","R");aController.displayMessageOnScreen(context,message);//通知用户sendNotification(上下文,消息);}私人无效sendNotification(Context context,String msg){字符串app_name = context.getResources().getString(R.string.app_name);mNotificationManager =(NotificationManager)此.getSystemService(Context.NOTIFICATION_SERVICE);PendingIntent contentIntent = PendingIntent.getActivity(this,0,新的Intent(this,ListOfClass.class),0);Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);NotificationCompat.Builder mBuilder =新的NotificationCompat.Builder(这个).setSmallIcon(R.mipmap.ic_launcher).setContentTitle(app_name).setStyle(新的NotificationCompat.BigTextStyle().bigText(msg)).setContentText(新消息").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,通知发送成功.");}} 

当我看到whatsapp,hiking和其他通知应用程序时,他将始终在后台线程中运行,但我的应用程序并非始终在后台运行.所以可能也是这个原因.我最近在使用android系统,请帮帮我.谢谢!

解决方案

小米手机中存在白名单的概念.因此,如果您在gcm的onReceive上输入日志,您会注意到gcm正在接收,但没有进一步处理.其原因是您的应用未列入白名单.

出于安全目的,Xiaomi会为每个应用程序禁用通知.使用清除器退出应用程序后,请按照以下步骤在后台接收消息.

  • 启用自动启动
  • 启用浮动和锁定屏幕通知

启用自动启动

  • 打开安全应用程序.
  • 获得权限,然后单击自动启动管理".
  • 添加/启用自动启动应用(例如Whatsapp).

启用浮动和锁定屏幕通知

  • 打开设置应用.
  • 单击通知",然后单击管理通知".
  • 点击您要查找的应用(例如WhatsApp).
  • 启用在通知阴影下显示/在锁屏和下拉菜单中显示".

作为参考,请查看:

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!

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

这篇关于应用未运行时,某些设备中未显示GCM推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆