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

2023-12-20

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

未显示通知的设备列表:

Redmi-2

lenovo

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" />

  <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 通知

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当应用程序未运行时,GCM 推送通知不会在某些设备中显示 的相关文章

随机推荐

  • 致命错误 C1083:无法打开包含文件:'iostream':没有这样的文件或目录

    我已重新安装 Visual Studio 2010 Professional 多次以尝试使其正常工作 我不得不卸载 Visual Studio 2012 Professional 因为它无法编译我们在课堂上所做的事情 我完全卸载了包括 SQ
  • xmlns、xmlns:xsi、xsi:schemaLocation 和 targetNamespace?

    对于以下 XML 片段
  • 实体框架和存储过程

    我注意到在实体框架设计器中 您可以映射插入 更新和删除操作的存储过程 有没有什么方法可以对 Select 操作执行此操作 或者数据库访问代码是否有一个新的方向 我们不再为基本的选择操作编写存储过程 我工作的公司非常坚持始终对每个数据库操作使
  • Python 中的双大于号

    我在查看一些Python源代码时 发现了这个 print gt gt sys stderr doc gt gt 是什么意思 我以前从未见过这样的语法 请参阅 打印 V 形 说明Python 2 7 文档 https docs python
  • 客户端Flash(SWF)应该如何与服务器端.NET通信?

    所以我在IIS7的服务器上运行ASP NET 我想我将使用 MVC 来处理一些静态页面和基本动态表单 但大多数客户端都是用 Flash ActionScript 编写的 在客户端和服务器之间构建 生成代理的最简单 最简洁 最DRY的方法是什
  • 如何在不写入文件的情况下将 DataVisualization.Charting.Chart 的图像获取到 iTextSharp.text.Image?

    我正在编写一个用于测量数据可视化的软件 为此 我使用 System Windows Forms DataVisualization Charting Chart 并且我确实知道我可以通过 ChartObj SaveImage 获取显示的图像
  • python dict.add_by_value(dict_2)?

    问题 gt gt gt a dict a 1 b 2 gt gt gt b dict b 3 c 2 gt gt gt c c a 1 b 5 c 2 因此 这个想法是通过 int float 值以最短的形式添加到字典中 这是我设计的一种解
  • SQL Server 如何决定隐式日期时间转换的格式?

    declare str datetime varchar 50 set str datetime 30 04 2012 19 01 45 30th April 2012 declare dt datetime datetime select
  • 不知道为什么 dcast() 这个数据集会导致变量丢失

    我有一个数据框 如下所示 id fromuserid touserid from country to country length 1 1 54525953 47195889 US US 2 2 2 54525953 54361607 U
  • 在日志文件中隐藏敏感/机密信息

    您将如何隐藏敏感信息以免进入日志文件 是的 您可以有意识地选择首先不记录敏感信息 但在一般情况下 您可能会盲目地记录失败时的错误消息或在调查问题等时跟踪消息 最终导致敏感信息登陆您的计算机 日志文件 例如 您可能尝试将包含客户信用卡号的订单
  • 如何忽略脚本内的 stdin 输入

    我有一个程序 myprogram 它在控制台上运行时等待输入键 当我们按下某个键后 它就会退出 我想在后台运行这个 myprogram 并忽略标准输入 我有一个示例脚本 示例 sh 脚本 myprogram exit 0 但当我跑步时 sh
  • 如何在 gdb 中中断 UBSan 报告并继续?

    GCC 和 Clang 的最新版本具有未定义行为清理程序 UBSan 它是一个编译标志 fsanitize undefined 添加运行时检测代码 出现错误时 会显示如下警告 packet ber c 1917 23 运行时错误 54645
  • 如何从日期/时间字段快速选择不同的日期,SQL Server

    我想知道是否有一个性能良好的查询来从 SQL Server 中带有日期时间字段的表中选择不同的日期 忽略时间 我的问题不是让服务器实际执行此操作 我已经看到这个问题 https stackoverflow com questions 887
  • ElasticSearch NEST 搜索

    当我尝试对 ElasticSearch 实例执行搜索时遇到意外错误 我正在关注这里的文档 http nest azurewebsites net nest quick start html http nest azurewebsites n
  • 如何在单击时展开文本区域

    我正在开发一个有文本区域的小项目 我需要帮助来使文本区域在鼠标单击时展开 就像 Twitter 和 Facebook 那样 文本区域首先应该看起来像一个文本字段 然后单击时应该展开 这可以在不使用 JavaScript jQuery 的情况
  • R中的时间序列

    我正在电子表格中跟踪我的体重 但我想通过使用 R 来改善体验 我试图在 R 中找到一些有关时间序列分析的信息 但没有成功 我这里的数据采用以下格式 date gt weight gt body fat percentage gt water
  • Angular 5 ngx-bootstrap 表单验证

    我正在读阿里 勒纳的书ng book在 Angular 5 上 我正在使用ngx bootstrap and Bootstrap 4 表单验证似乎并不像勒纳先生实现的那样工作 我不确定这是否是一个限制ngx bootstrap 有人知道吗
  • 显示数组内容的问题

    下面的代码将内容和图像保存到 varcontent i content and vacontent i images 然而 我在正确显示内容和图像的循环之外唯一得到的是 varcontent i content Print r 显示数组的所
  • ValueError:需要解压的值太多(预期为 2)

    在我正在使用的Python教程书中 我输入了一个示例同时分配 当我运行程序时 我得到了前面提到的 ValueError 但不明白为什么 这是代码 avg2 py A simple program to average two exam sc
  • 当应用程序未运行时,GCM 推送通知不会在某些设备中显示

    我在我的应用程序中实现了 GCM 推送通知 并且已成功完成 但在某些设备中 当应用程序关闭时 它不会显示通知 未显示通知的设备列表 Redmi 2 lenovo Gionee 谁能解释一下问题是什么以及我如何解决它 这是我的清单