2014-06-19 140 views
19

您知道当应用程序完全关闭时是否可以接收来自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); 
} 
} 

当应用程序是开放的:OK

当应用程序在后台:

w ^当该应用程序被用户强行关闭时:通知未到达

问题是什么?

非常感谢。

+0

@ user3757628:你是怎么解决这个问题的? – user1090751

回答

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权限。 这允许使用PowerManagerWakeLocks来保持处理器免于睡眠或屏幕变暗。 但是,如果您扩展BroadcastReceiver,则该应用程序关闭后将无法工作。

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

但它不适用于棒棒糖设备,它在棒棒糖设备上工作正常。我需要为棒棒糖设备添加任何东西吗? – saikrupa

5

它不会对杀害从任务管理器应用程序的工作,但如果你只是从最近滑出工作。我试图通过杀死whatsapp并要求某人发送msg给我,但我没有收到任何通知。然后我启动了应用程序,并收到通知。

+6

即使我将其从小米等设备上的近期应用程序中滑出,我也没有收到通知。这些手机具有“自动启动”功能,如果我为自己的应用程序启用自动启动功能,即使将应用程序从最近的应用程序中移除,我也会收到推送通知。但我不希望用户手动为我的应用启用自动启动。还有其他应用程序,例如已启用自动启动的whatsapp,facebook。应用程序安装时应该如何自动启用自动启动? –

+0

@NiteshKhatri对不起,没有关于自动启动的线索。 – berserk

+0

我也在华为手机中找到同样的问题。实际上,当从任务列表中清除应用程序时,它没有收到任何通知。事实上,当我们这样做时,它会杀死应用程序。你有没有找到解决办法? –

19

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

Infact,广播接收机是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

+0

最好的答案 –

+3

但它不适用于棒棒糖设备,它在棒棒糖设备上运行良好。我需要为棒棒糖设备添加任何东西。 – saikrupa

+0

它不适用于联想设备..对于其他设备,它工作正常 –

0

我一直在尝试其他手机和第二工作完美,即使电话关机。首先有一个房间是不允许被通知当应用程序被关闭...... 感谢所有

18

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

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

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

+0

有没有任何方法可以通知用户,以至于直到应用程序打开时才能收到通知,因为例如,自动启动管理器在安装时禁用应用程序。所以GCM推送不工作。 –

+1

它确定..如果应用程序关闭时通知未到达。但是当应用程序再次启动时,我没有收到这些通知。它永久丢失了吗? – shijin

+0

@yuen shi:是的。当您的应用程序关闭时,仍会将GCM推送消息传送到您的设备。由于强制关闭的应用程序,你没有得到任何通知。 – Rana

0

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

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

0

火力地堡API有两种类型的消息,他们称他们为:

  • 通知
  • 数据

here

找到更多的信息要点:你不能从发送数据有效负载消息Firebase控制台,控制台只发送通知消息。所以我提到了如何使用邮递员发送推送通知,请按照下面的步骤进行。

您必须返回,因为数据有效载荷的数据有效载荷推送消息 - 不要紧,你的应用是否在前台或后台或被杀,这些消息会一直传递到onMessageReceived()方法。

的Android 8.0奥利奥如果应用程序被关闭,然后通知没有收到这是因为打盹模式,并优化电池,你就必须关闭电池优化为所有应用或特定应用程序。

关闭电池优化你的应用程序通过下面的步骤:

设置>>电池>>电池优化>>查找应用>>选择>>如果在优化单击不优化>>尝试推使用发送推送通知通知

邮递员

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

步骤2:将此两成部首

授权:键= 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" 
} 
} 

希望这会有所帮助..

享受:) :)

0

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

enter image description here

相关问题