繁体   English   中英

当应用未运行时,某些设备中未显示GCM推送通知

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

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

通知未显示的设备一览:

红米手机-2

联想

金立

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

在这里我的清单: -

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

这里我的服务名称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.");
    }
}

当我看到whatsapp,远足和其他通知应用程序时,他将始终在后台线程中运行,但我的应用程序并不总是在后台运行。 所以可能也是这个原因。 我最近正在研究android请帮帮我。谢谢!

我遇到了与Redmi-2类似的问题。 代码中没有问题,但这是由制造商提供的自定义UI(如MIUI 6)引起的。因此要启用GCM通知

转到安全应用>>点击权限>>点击自动启动并为您的应用启用自动启动。

这有两个主要原因

1 - 某些设备不允许您在像redmi-2这样的背景上运行服务(几乎在所有xiaomi设备上)。 甚至什么应用程序都无法在它们上正常工作,除非用户允许它们转到安全应用程序>>点击权限>>点击自动启动并启用自动启动whatsapp等。在这种情况下,你所能做的就是显示详细信息这对应用程序启动时的用户。 并打开该屏幕并引导用户(如果可能)像干净的主人。

2-第二个原因是它无法正常使用其Google Play服务应用未正确安装的手机(对GCM而言必不可少)。 您也无法在这些设备上执行任何操作。 在这种情况下,您唯一能做的就是向用户显示一些关于此的消息。

因此,根据我的经验,总会有许多用户(但很小的百分比)不会接受GCM推送。

小米的手机中有白名单的概念。 因此,如果您将登录onGceive放入gcm,您会注意到gcm正在接收,但它没有进一步处理。 这是因为您的应用不会列入白名单。

出于安全目的,小米禁用每个应用程序的通知。 按照以下步骤,一旦用清理工具退出应用程序,就会在后台接收消息。

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

启用AutoStart

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

启用浮动和锁定屏幕通知

  • 打开设置应用。
  • 单击“通知”,然后单击“管理通知”。
  • 点击您要查找的应用程序(例如WhatsApp)。
  • 在通知窗帘中启用显示/在锁屏和下拉选项中显示。

如需参考,请查看: http//support.hike.in/entries/55998480-Im-not-getting-notification-on-my-Xiaomi-Phone-For-MIUI-6-

我得到了这个成功..希望它有所帮助..

面对同样的问题,我们做的唯一事情是教育xiaomi用户执行@ anup-dasari提到的步骤,并将gcm优先级设置为高,以及将来可能有持久服务的可能性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM