应用关闭时推送通知

时间:2014-06-19 18:10:53

标签: android notifications 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>

广播接收器

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

应用程序打开时:确定

当应用在后台时:确定

当用户强行关闭应用时:通知无法到达

有什么问题?

非常感谢。

8 个答案:

答案 0 :(得分: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" />

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

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

上面的代码示例使用了一个名为BroadcastReceiver的专用WakefulBroadcastReceiver,可以确保设备在服务正常工作时不会进入休眠状态。

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

答案 1 :(得分:18)

  

当用户完全关闭应用时:通知无法到达

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

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

答案 2 :(得分:5)

它不能用于从任务管理器中删除应用程序,但如果你只是将它从最近的地方滑出来就可以工作。我尝试通过杀死它并让某人向我发送一个消息来做whatsapp,但我还没有得到任何通知。然后我启动了应用程序,然后收到通知。

答案 3 :(得分: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);

}
}

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

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

答案 4 :(得分:2)

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

  • 通知
  • 数据

here

中查找更多信息

重要提示:您无法从 Firebase控制台发送数据有效内容消息,控制台仅发送通知消息。所以我已经提到了如何使用邮递员发送推送通知,请按照以下步骤进行。

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

Android 8.0 Oreo 中,如果应用程序已关闭,则无法收到通知由于DOZE模式和电池优化,您只需关闭所有应用或特定应用的电池优化。

通过以下步骤为您的应用关闭电池优化:

<强>设置&gt;&GT;电池&gt;&gt;电池优化&gt;&gt;找到你的应用&gt;&gt;选择&gt;&gt;如果优化,请点击“不要优化”&gt;&gt;尝试推送通知

使用邮递员

发送推送通知

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

第2步:将这两个设置为标题

授权:key = AIzaSyBuRl1Ikz3VXFvU7xW9mmg51lJ3uDSH

内容类型:application / json

第3步:发送此json

{
 "to" : "eb9HgFulyzU:APA91bFMLReWWwilNFfJ1fnXZ0A2PhJAYsabg-UcK_dHgHQcstTzcxLs4_mqgOmZtUiyLMne4JaOG7f8KH7pWxB7JugOGCCYgjd4fUynRBKZvNUgjaj2UdJB2Ux8VocszuUydCX",
 "data" : {
     "message" : "First Notification",
     "title": "Push Notification",
     "key_1" : "Key 1 value",
     "key_2" : "Hello"
 }
}

希望这会有所帮助..

享受:) :))

答案 5 :(得分:1)

经过一些测试,我在这个问题上做了一些测试,我发现它在应用程序关闭方面表现不同。如果在电缆连接到设备时关闭应用程序,则会在关闭应用程序时阻止每个通知。 但是,如果您从设备上取下电缆并关闭应用程序,当您发送通知时,一切都会再次正常工作!

如果你得到一个&#34;等待调试器&#34;弹出消息,只需重启设备然后发送测试通知!

答案 6 :(得分:0)

我一直在尝试另一部手机,第二部手机完美无缺,即使手机关机也是如此。第一个有一个房间,当应用程序关闭时不允许通知... 感谢所有

答案 7 :(得分:0)

我在Oneplus手机上遇到了同样的问题。正如上面许多人所提到的,问题是有力的应用程序终止。在我的情况下,它是由于“最近的应用程序管理”设置完成的,如下所示。一旦我将其更改为“正常清除”设置,一切都开始正常工作。

enter image description here

相关问题