我想将推送通知发送到我的Android设备并将它们放置,以致用户很难错过推送通知。特别是如果应用程序没有运行或处于后台。我已经知道如何在应用程序运行时处理gcm推送通知。但是,如果应用程序未运行,我感兴趣的是如何自定义地显示gcm消息。
这就是我现在的情况:
这就是我想要的:
在网上进行了广泛的搜索之后,我还没有找到任何解释如何做到这一点的资源。
有人能为我提供一些指导吗?
答案 0 :(得分:3)
您可以使用WakefulBroadcastReceiver
来阻止手机休眠并检查GCM通知,即使您的应用未开启也是如此。
以下是实现目标的步骤
第1步:您必须在清单中添加WAKE_LOCK
,INTERNET
和其他权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="your.package.name.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="your.package.name.permission.C2D_MESSAGE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
第2步:在WakefulBroadcastReceiver
标记
GCMMessageHandler
和<application>
<receiver android:name=".gcm.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.hmkcode.android.gcm"/>
</intent-filter>
</receiver>
<service android:name=".gcm.GcmMessageHandler"/>
第3步: GcmBroadcastReceiver
的定义低于
package your.package.name.gcm;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
第4步: GcmMessageHandler
的定义是
package your.package.name.gcm;
import android.app.IntentService;
import android.app.Notification;
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.google.android.gms.gcm.GoogleCloudMessaging;
import your.package.name.R;
import your.package.name.SplashScreen;
public class GcmMessageHandler extends IntentService {
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
final Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if(!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.e("GCM", "Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.e("GCM", "Deleted messages on server: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification(extras);
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(Bundle extras) {
try {
String message = extras.getString("message");
if(message != null) {
Log.e(tag, "Notification received " + message);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("TITLE");
mBuilder.setContentText(message);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, new Intent(this, SplashScreen.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setDefaults(Notification.DEFAULT_ALL); // this line sets the default vibration and sound for notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
} else {
Log.d(tag, "Message is empty.");
}
} catch (Exception ex) {
Log.e(tag, Log.getStackTraceString(ex));
}
}
}
希望它会帮助你。