Android 应用关闭时推送通知

Android 应用关闭时推送通知,android,notifications,push,Android,Notifications,Push,你知道当应用程序完全关闭时,是否有可能收到来自谷歌云消息的通知 我知道它是打开的还是在后台的是的,但它可以通过任何方式编程来接收它们吗 编辑: 当应用程序关闭时,我将继续而不接收通知 <uses-permission android:name="android.permission.WAKE_LOCK" /> 我附加了代码,以防我有一个错误,我不看它 显示 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns

你知道当应用程序完全关闭时,是否有可能收到来自谷歌云消息的通知

我知道它是打开的还是在后台的是的,但它可以通过任何方式编程来接收它们吗

编辑:

当应用程序关闭时,我将继续而不接收通知

<uses-permission android:name="android.permission.WAKE_LOCK" />
我附加了代码,以防我有一个错误,我不看它

显示

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

广播接收机

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);
}
}
package.com.something;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v4.app.NotificationCompat;
导入android.util.Log;
导入com.activities.sign;
导入com.google.android.gcm.GCMBaseIntentService;
导入com.objects.Globals;
公共类GCMinentService扩展了GCMBaseintService{
私有静态最终字符串TAG=“GGM FRAB”;
私人包袱;
公共GCMinentService(){
超级(全局。发送者\u ID);
}
@凌驾
公共空间{
日志d(标签“terminando servicio”);
}
@凌驾
受保护的void onRegistered(上下文、字符串注册ID){
Log.i(标记“onRegistered:registrationId=“+registrationId”);
}
@凌驾
未注册受保护的void(上下文、字符串注册ID){
Log.i(标记“onUnregistered:registrationId=“+registrationId”);
}
@凌驾
受保护的void onMessage(上下文、意图数据){
extras=data.getExtras();
String message=extras.getString(“msg”);
Log.d(“*******”,消息);
发送通知(消息);
}
@凌驾
受保护的void onError(上下文arg0,字符串errorId){
Log.e(标记“onError:errorId=“+errorId”);
}    
}
包com.something;
导入android.app.Activity;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v4.content.WakefulBroadcastReceiver;
公共类GGMBroadcastReceiver扩展WakefulBroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
//明确指定gcminentservice将处理该意图。
ComponentName comp=新组件名(context.getPackageName(),gcminentservice.class.getName());
//启动服务,在设备启动时保持设备处于唤醒状态。
startWakefulService(上下文,(intent.setComponent(comp));
setResultCode(Activity.RESULT\u OK);
}
}
应用程序打开时:OK

当应用程序处于后台时:Ok

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

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);
    }
}
有什么问题

非常感谢。

我刚刚完成了一个GCM应用程序。 如果关闭应用程序,它仍然可以正常工作。问题是你应该已经打开了应用程序一次。 创建两个类来实现此目标:
GcmBroadcastReceiver
GcnIntentService
。 查找更多信息

这里是诀窍

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);

}
}
正在扩展的类是WakeFulBroadcastReceiver。Manifest.xml中有唤醒锁定权限。 这允许使用
PowerManager
WakeLocks
来防止处理器睡眠或屏幕变暗。 但是,如果扩展
BroadcastReceiver
,则该应用程序关闭后将无法工作

<uses-permission android:name="android.permission.WAKE_LOCK" />


它不会从task manager中删除应用程序,但如果您只是将其从最近的应用程序中滑出,它就会起作用。我试着在whatsapp上杀掉它,让别人给我发一条消息,但我没有收到任何通知。然后我启动了应用程序,收到了通知。

是的,可以“在应用程序完全关闭时接收来自谷歌云消息的通知”

事实上,广播接收器是GCM用来传递消息的机制。 您需要实现并在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" />
当GCM向您的设备发送消息时,
BroadcastReceiver
将接收消息并调用
onReceive()
函数,在该函数中,您可以启动一项服务来实际执行预期任务

上面的代码示例使用一个名为WakefulBroadcastReceiver的专用广播接收器,确保设备在服务工作时不会进入睡眠状态


请参阅Android官方页面了解同样的信息:

我一直在尝试另一款手机,第二款手机工作正常,即使手机关机。第一个房间不允许在应用程序关闭时收到通知。。。 多谢大家

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

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


进一步阅读:

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


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

Firebase API有两种类型的消息,它们调用它们:

  • 通知
  • 资料
从中查找更多信息

重要提示:您不能从Firebase控制台发送数据有效负载消息,控制台仅发送通知消息。所以我提到了如何使用邮递员请求发送推送通知