Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GCM无法接收消息_Android_Firebase_Google Cloud Messaging - Fatal编程技术网

Android GCM无法接收消息

Android GCM无法接收消息,android,firebase,google-cloud-messaging,Android,Firebase,Google Cloud Messaging,我的应用程序使用GCM发送通知消息,我的问题是: 我的应用程序可以在应用程序处于前台时接收通知消息,但当应用程序处于后台或被杀死时,我的应用程序无法接收通知消息 我想自定义通知视图,我不希望gcm发送通知。最好切换到Firebase云消息(FCM)。它处理应用程序的前台和后台案例 与GCM不同的是,在GCM中还必须实现广播接收器来接收消息,在FCM中,我们使用扩展FirebaseMessagingService的服务。您的服务应该覆盖onMessageReceived和onDeletedMess

我的应用程序使用GCM发送通知消息,我的问题是:

我的应用程序可以在应用程序处于前台时接收通知消息,但当应用程序处于后台或被杀死时,我的应用程序无法接收通知消息


我想自定义通知视图,我不希望gcm发送通知。

最好切换到Firebase云消息(FCM)。它处理应用程序的前台和后台案例

与GCM不同的是,在GCM中还必须实现广播接收器来接收消息,在FCM中,我们使用扩展
FirebaseMessagingService
的服务。您的服务应该覆盖
onMessageReceived
onDeletedMessages
回调

onMessageReceived
适用于大多数邮件类型,但以下例外情况除外:

  • 当你的应用程序在后台时发送通知。在这个 在这种情况下,通知将发送到设备的系统托盘。A. 默认情况下,用户点击通知会打开应用程序启动器
  • 同时具有通知和数据负载的消息,包括后台和 前景。在这种情况下,通知将传递给 设备的系统托盘,数据有效负载在extras中交付 您的启动器活动的意图

有关更多信息,请阅读

使用
服务
在后台处理您的代码,即使关闭了应用程序

public class MyService extends Service {

 static Activity activity;

public void SendNotification(String message) {

   Intent  resultIntent = new Intent(this, YourActivity.class); //Display activity when user click notification
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), resultIntent, 0);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_lancher)
            .setContentTitle("title")
            .setContentText(message)
            .setSound(alarmSound)
            .setContentIntent(pIntent);

    NotificationManager mNotificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}


@Override
public void onCreate() {
    super.onCreate();

    // Start GCM code here 
    // or
    SendNotification("hi!);        
 }
}
AndroidManifest.xml

 <service
        android:name=".MyService"
        android:exported="false" />

谢谢,但我们的项目必须使用GCM,所以我想知道如何解决这个问题?谢谢,但你的回答无法解决我的问题,也许你不理解我的问题。请用你编写的代码编辑你的帖子,以便更清晰。我使用谷歌云消息推送通知消息,当应用程序被终止或处于后台时,google cloud message无法接收消息。即使应用程序已关闭,该服务也主要为后台操作开发。因此,请尝试在服务内部而不是在正常活动或片段内部编写GCM代码。您可能希望在此尝试建议的解决方案,看看它是否对您有帮助。正如OP提到的,当应用程序在后台时,需要服务器端进行操作。数据部分将自动保存在意图中,并发送到内容筛选器中包含该操作的活动。请参阅本文以了解更多信息。
Intent serviceIntent = new Intent(YourActivity.this, MyService.class);
startService(serviceIntent);