应用关闭时推送通知

use*_*628 30 notifications android push

您知道在应用程序完全关闭时是否可以从Google云消息接收通知?

我知道它是开放的还是在后台是的,但它可以以任何方式编程以便接收它们吗?

编辑:

应用程序关闭时,我会继续收到通知

我附上代码,以防我有错误,我不看.

表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frab"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<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" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.frab.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="com.frab.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.frab.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name=".GGMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.something" />
</intent-filter>
</receiver>

<service android:name=".GCMIntentService" />
</application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

广播接收器

package com.something;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.activities.SignIn;
import com.google.android.gcm.GCMBaseIntentService;
import com.objects.Globals;

public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GGM <-----> FRAB";
private Bundle extras;

public GCMIntentService() {
super(Globals.SENDER_ID);
}

@Override
public void onDestroy() {
Log.d(TAG, "terminando servicio");
}

@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "onRegistered: registrationId=" + registrationId);
}

@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "onUnregistered: registrationId = " + registrationId);
}
@Override
protected void onMessage(Context context, Intent data) {
extras = data.getExtras();
String message = extras.getString("msg");
Log.d("******", message);
sendNotification(message);
}

@Override
protected void onError(Context arg0, String errorId) {
Log.e(TAG, "onError: errorId = " + errorId);
}    
}

package com.something;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

public class GGMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Run Code Online (Sandbox Code Playgroud)

当应用程序打开时:好的

当应用程序处于后台时:好的

当用户强行关闭应用程序时:通知不会到达

问题是什么?

非常感谢你.

Sah*_*jaj 21

是的,当应用程序完全关闭时,可能会"收到来自Google云消息的通知".

事实上,广播接收器是GCM用于传递消息的机制.您需要实现BroadcastReceiver并在AndroidManifest.xml中声明它.

请参阅以下代码段.

AndroidManifest.xml中

<receiver
    android:name=".GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <!-- Receives the actual messages. -->
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.google.android.gcm.demo.app" />
    </intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
Run Code Online (Sandbox Code Playgroud)

Java代码

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

当GCM向您的设备发送消息时,BroadcastReceiver将接收消息并调用该onReceive()功能,其中您可以启动服务以实际执行预期的任务.

上面的代码示例使用一个专门的BroadcastReceiver被调用WakefulBroadcastReceiver,它可以确保设备在服务正在运行时不会进入休眠状态.

请参阅官方Android页面:https://developer.android.com/google/gcm/client.html

  • 但它不适用于棒棒糖前设备,它在棒棒糖设备中工作正常.我需要为棒棒糖设备添加任何东西. (3认同)

laa*_*lto 18

当应用程序被用户关闭时:通知不会到达

这是Android平台的一个功能.用户强制停止应用程序会将应用程序置于停止状态,并且不会运行任何代码,包括清单中声明的​​任何广播接收器.只有当用户明确启动应用程序时,它才处于接收器被触发的状态.

如需进一步阅读:http://www.doubleencore.com/2014/06/effects-android-application-termination/

  • 那么whatsapp应用程序和其他聊天应用程序如何工作? (11认同)
  • 那是不正确的。应用程序关闭后,通知等服务将继续运行。 (2认同)

Zez*_*ez3 9

在对此问题进行了一些测试后,我发现它在应用程序关闭方式上的行为有所不同。如果您在电缆连接到设备时关闭应用程序,它会在应用程序关闭时阻止每个通知。但是,如果您从设备上取下电缆并关闭应用程序,当您发送通知时,一切都会恢复正常!

如果您收到“等待调试器”弹出消息,只需重新启动设备,然后发送测试通知!

  • 我将提供更多详细信息:如果您正在调试应用程序,通知将首先起作用。然后,如果您关闭该应用程序(从最近滑动),它们将无法工作。然后,您必须从设备手动重新启动应用程序(而不是通过调试)。然后您可以关闭它,通知将到达。当您关闭调试应用程序时,它的所有服务似乎都停止了。我因此失去了 2 天。 (7认同)

Dha*_*ani 7

Firebase API 有两种类型的消息,他们称之为:

  • 通知
  • 数据

这里找到更多信息

重要提示:您不能从Firebase 控制台发送数据有效负载消息,控制台仅提供通知消息。所以我已经提到了如何使用邮递员发送推送通知,请按照以下步骤操作。

由于数据负载,您必须发送带有数据负载的推送消息 - 无论您的应用程序是在前台还是后台或被终止,这些消息都将始终传递到onMessageReceived()方法。

Android 8.0 Oreo 中,如果应用程序关闭,则不会收到通知 这是因为 DOZE 模式和电池优化,您只需关闭所有应用程序或特定应用程序的电池优化。

按照以下步骤关闭应用的电池优化:

设置>>电池>>电池优化>>找到你的应用>>选择>>如果优化点击不优化>>尝试推送通知

使用邮递员发送推送通知

第 1 步:https : //fcm.googleapis.com/fcm/send

第2步:将这两个设置为Header

授权:密钥=AIzaSyBuRl1Ikz3VXFvU7xW9mmg51lJ3uDSH

内容类型 :应用程序/json

第 3 步:发送此 json

{
 "to" : "eb9HgFulyzU:APA91bFMLReWWwilNFfJ1fnXZ0A2PhJAYsabg-UcK_dHgHQcstTzcxLs4_mqgOmZtUiyLMne4JaOG7f8KH7pWxB7JugOGCCYgjd4fUynRBKZvNUgjaj2UdJB2Ux8VocszuUydCX",
 "data" : {
     "message" : "First Notification",
     "title": "Push Notification",
     "key_1" : "Key 1 value",
     "key_2" : "Hello"
 }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助..

享受 :) :)


ber*_*erk 5

它不适用于从任务管理器中删除应用程序,但如果你只是将它从最近的位置移开,则可以正常工作.我尝试通过杀死它并要求某人向我发送一个消息来做whatsapp,我没有收到任何通知.然后我启动了应用程序,我收到了通知.

  • 即使我在小米这样的设备上将其从最近的应用程序中删除,我也不会收到通知.这些手机中有一个"自动启动"功能,如果我为我的应用启用了自动启动,那么即使我将应用从最近的应用中移开,我也会收到推送通知.但我不希望用户为我的应用程序手动启用自动启动.还有其他应用程序,如whatsapp,facebook已启用自动启动.安装应用程序时,我该如何自动启用自动启动? (8认同)
  • @Md.SajedulKarim 这似乎不是正常行为。清除最近的应用程序不应杀死应用程序(在内存不足的情况下,操作系统可以杀死任何应用程序,但我认为它不会在您将其滑开后立即发生)。我会说设备操作系统有问题(可能是供应商的错) (2认同)

小智 2

我刚刚完成了一个 GCM 应用程序。即使您关闭应用程序,它仍然可以正常工作。问题是您应该已经打开过该应用程序一次。创建两个类来实现此目标:GcmBroadcastReceiverGcnIntentService。查找更多信息 http://developer.android.com/google/gcm/index.html

这是窍门

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);

}
}
Run Code Online (Sandbox Code Playgroud)

正在扩展的类是WakeFulBroadcastReceiver. 您的 Manifest.xml 中有一个 WAKE_LOCK 权限。这允许使用PowerManager WakeLocks来防止处理器休眠或屏幕变暗。但是,如果您扩展BroadcastReceiver,则在应用程序关闭后它将不起作用。

<uses-permission android:name="android.permission.WAKE_LOCK" />
Run Code Online (Sandbox Code Playgroud)