【问题标题】:Why my GcmListenerService.onMessageReceived is not called when app is not running?为什么应用程序未运行时不调用我的 GcmListenerService.onMessageReceived?
【发布时间】:2016-03-19 12:15:02
【问题描述】:

我需要在我的应用程序中创建并显示一些自定义推送通知(需要传递一些数据并在单击时打开一些特定的活动)。我做了所有像here描述的事情。

当应用程序运行时,一切都按预期工作。但是当它在后台时,我的服务没有启动,我收到一个推送通知,将我带到应用程序的启动屏幕。

我进行了很多研究,但没有找到对这种行为的解释。 谁能告诉我为什么应用程序在后台时忽略我的服务?

这是在我的清单文件中:

<permission
    android:name="com.locdel.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.locdel.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

......................

<application
    android:name=".LocdelApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.locdel" />
        </intent-filter>
    </receiver>

    <service
        android:name="com.locdel.gcm.LocdelGcmListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name="com.locdel.gcm.LocdelIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>
    <service
        android:name=".gcm.RegistrationIntentService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

.......................

我什至尝试扩展我自己的唤醒广播接收器并明确启动服务。这是自定义receiver的onReceive方法,实际调用了,但是服务没有重新启动:

@Override
public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent(context, LocdelGcmListenerService.class);
    serviceIntent.setAction("com.google.android.c2dm.intent.RECEIVE");
    serviceIntent.putExtras(intent.getExtras());
    startWakefulService(context, serviceIntent);
    setResultCode(Activity.RESULT_OK);
}

【问题讨论】:

  • 您好,您能否说明一下您是如何在 manifest.xml 中声明该服务的以及该服务的代码?
  • 更新了我的问题。服务中的代码实际上并不重要。我已经覆盖了 onMessageReceived 方法并将一些日志放在那里。所以我知道服务根本没有启动
  • 你在清单文件中提到的权限是什么?
  • 再次更新,伙计们,我说我做了所有描述的事情。
  • 您的问题有解决方案吗?如果是,请帮助我,因为我面临同样的问题。

标签: android google-cloud-messaging gcmlistenerservice


【解决方案1】:

【讨论】:

  • 它没有。正如我提到的,当应用程序运行时一切正常。
【解决方案2】:

当收到这样的消息时,您需要检查活动是否正在运行

 ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;
    if(componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){

        updateMyActivity(message);  //send broadcast to notify app



        //Activity Running
//            Send a broadcast with the intent-filter which you register in your activity
//            where you want to have the updates
        }
        else{
            //Activity Not Running
            //Generate Notification
            sendNotification(message);
        }

【讨论】:

  • 在哪里?问题是没有收到消息。
  • @shift66 我有点不清楚应用程序是否在后台时调用 onReceived ?
  • 是的,广播接收器的 onReceive 被调用,我什至在那里手动启动服务,由于某种原因它没有启动
【解决方案3】:

尝试从 onReceive 以这种方式调用通知

 private void sendNotification(String message) {
            Intent intent = new Intent(this,FullMapActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //        int color = getResources().getColor(R.color.my_notif_color);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            /*PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent,
                    PendingIntent.FLAG_ONE_SHOT);*/

            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {



                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.headerlogo)
                        .setContentTitle("hey..! your booking is confirmed")
                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);


                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
            }
            else
            {

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.headerlogo)
                        .setContentTitle("hey..! your booking is confirmed")
                        .setContentText(message)
                        .setAutoCancel(true)
    //                    .setColor(color)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);


                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
            }
        }

【讨论】:

    【解决方案4】:

    https://github.com/google/gcm/issues/63

    在上面的链接中明确提到,如果存在“通知”有效负载,那么 android 将触发通知。尝试删除“通知”有效负载并仅发送“数据”有效负载,您的 onMessageReceived 应该会被调用。

    【讨论】:

    • 如何处理iOS?似乎iOS只处理“通知”有效载荷
    • 是的。您无法控制 IOS 中的通知。发起通知的是服务器,而通知只是显示在手机中。服务器将不得不指定目标视图和其他信息。应用程序在被杀死时肯定无法处理它。当它处于后台模式时它可能会处理它(虽然不确定)。这就是 Whatsapp 为收到的每条消息发送不同通知的原因。在 Android 中,它将累积它们并将它们显示为一个。希望这会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多