Java Android后台列表的云消息
你好,我的问题是这个。 我有一个Android应用程序,可以接收dotifiche for Google Cloud Messaging,直到手机屏幕关闭接收通知,但我一关闭它就什么也得不到,你应该公开代码,因为现在你有一些想法了吗?Java Android后台列表的云消息,java,android,service,background,push-notification,Java,Android,Service,Background,Push Notification,你好,我的问题是这个。 我有一个Android应用程序,可以接收dotifiche for Google Cloud Messaging,直到手机屏幕关闭接收通知,但我一关闭它就什么也得不到,你应该公开代码,因为现在你有一些想法了吗? <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ex
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lucabigoni.progettoclientserverolbia" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="com.example.lucabigoni.progettoclientserverolbia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.lucabigoni.progettoclientserverolbia.C2D_MESSAGE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<uses-permission android:name="android.permission." />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity.Prova"
android:label="@string/ciao" >
</activity>
<activity
android:name=".Activity.MainActivity"
android:label="main" >
</activity>
<activity
android:name=".Activity.manu"
android:label="@string/title_activity_manu" >
</activity>
<activity
android:name=".Activity.Visualizza"
android:label="@string/title_activity_visualizza" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyADrZavHTaGfd8_pTyqOMQLMQg2jfJT_S4" />
<activity
android:name=".Activity.mapOlbia"
android:label="@string/title_activity_map_olbia" >
</activity>
<activity
android:name=".Activity.Immagine_nave"
android:label="@string/title_activity_immagine_nave" >
</activity>
<activity
android:name=".Activity.Splash_Scree_caricamento_Immagine"
android:label="@string/title_activity_splash__scree_caricamento__immagine" >
</activity>
<activity
android:name=".Activity.Splash_screen_caricamento_dati"
android:label="@string/title_activity_splash_screen_caricamento_dati" >
</activity>
<activity
android:name=".Activity.AscoltoNave"
android:label="@string/title_activity_ascolto_nave" >
</activity>
<activity
android:name=".Activity.Scegli"
android:label="@string/title_activity_scegli" >
</activity>
<activity
android:name=".Activity.SceltaTipologia"
android:label="@string/title_activity_scelta_tipologia" >
</activity>
<receiver
android:name=".ricevitorenotifiche.NotificationReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.android.recognition" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="it.html.googleservices.push" />
</intent-filter>
</receiver>
<intentservice
android:name=".ricevitorenotifiche.GcmIntentService"
android:enabled="true"
android:exported="false"
android:process=":questo" >
</intentservice>
<receiver
android:name=".ricevitorenotifiche.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.thanksandroid.example.gcmdemo" />
</intent-filter>
</receiver>
</application>
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v4.content.WakefulBroadcastReceiver;
导入android.util.Log;
公共类GcmBroadcastReceiver扩展WakefulBroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
//明确指定gcminentservice将处理该意图。
Log.e(“Ricevuto”,“CIAO SONO PRONTO”);
ComponentName comp=新的ComponentName(context.getPackageName(),
gcminentservice.class.getName());
//启动服务,在设备启动时保持设备处于唤醒状态。
startWakefulService(上下文,(intent.setComponent(comp));
setResultCode(Activity.RESULT\u OK);
}
}
包com.example.lucabigoni.progettoclientserverolbia.ricevitoreNotification;
导入com.example.lucabigoni.progettoclientserverolbia.Activity.MainActivity;
导入com.example.lucabigoni.progettoclientserverolbia.R;
导入com.google.android.gms.gcm.GoogleCloudMessaging;
导入android.app.Activity;
导入android.app.IntentService;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.SystemClock;
导入android.support.v4.app.NotificationCompat;
导入android.support.v4.content.WakefulBroadcastReceiver;
导入android.util.Log;
公共类GCMinentService扩展了IntentService/*实现了WakefulBroadcastReceiver*/{
语境;
公共静态int通知_ID=1;
私人通知经理通知经理;
通知建筑商;
公共静态最终字符串TAG=“GCM Demo”;
公共GCMinentService(){
超级(“GCMinentService”);
//TODO自动生成的构造函数存根
}
/*
@凌驾
公共void onReceive(上下文、意图){
//TODO自动生成的方法存根
//明确指定gcminentservice将处理该意图。
ComponentName comp=新的ComponentName(context.getPackageName(),
gcminentservice.class.getName());
//启动服务,在设备启动时保持设备处于唤醒状态。
startWakefulService(上下文,(intent.setComponent(comp));
setResultCode(Activity.RESULT\u OK);
}*/
@凌驾
受保护的手部内容无效(意图){
//TODO自动生成的方法存根
Bundle extras=intent.getExtras();
String msg=intent.getStringExtra(“消息”);
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
字符串messageType=gcm.getMessageType(intent);
Log.e(“qua到达”,“qua”);
如果(!extras.isEmpty()){
如果(谷歌云通讯)。
消息类型发送错误。等于(消息类型)){
sendNotification(“发送错误:+extras.toString());
}否则如果(谷歌云信息)。
消息类型已删除。等于(消息类型)){
sendNotification(“服务器上已删除的邮件:”+
附加的toString());
//如果是常规GCM消息,请执行一些操作。
}否则如果(谷歌云信息)。
MESSAGE_TYPE_MESSAGE.equals(messageType)){
//此循环表示正在执行某些工作的服务。
对于(int i=0;i
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Explicitly specify that GcmIntentService will handle the intent.
Log.e("Ricevuto","CIAO SONO PRONTO");
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.example.lucabigoni.progettoclientserverolbia.ricevitorenotifiche;
import com.example.lucabigoni.progettoclientserverolbia.Activity.MainActivity;
import com.example.lucabigoni.progettoclientserverolbia.R;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.Activity;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
public class GcmIntentService extends IntentService /*implements WakefulBroadcastReceiver */{
Context context;
public static int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM Demo";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
/*
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// 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);
}*/
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
Log.e("qua arrivato","qua");
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
sendNotification(msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Log.e("qua arrivato","qua2");
Intent myintent = new Intent(this, MainActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
NOTIFICATION_ID++;
}
}