我想将推送通知发送到我的 android 设备并将它们放置在用户很难错过推送通知的位置。尤其是当应用程序未运行或处于后台时。我已经知道如何在应用程序运行时处理 gcm 推送通知。但是,我对如何在应用程序未运行时自定义显示 gcm 消息很感兴趣。
我现在的情况是这样的:
- 应用已关闭/未运行
- 我用我的 python 脚本发送了一个推送通知,它在我的设备上收到了
- 播放声音屏幕保持黑暗,无振动
- 当按下 sleep /唤醒按钮时,我会看到一条小通知
这就是我想要的:
- 应用已关闭/未运行,设备已锁定,屏幕已关闭
- 我通过 gcm 向我的设备发送推送通知
- 当我的设备收到消息时,应该会发生以下情况
- 手机应该振动
- 手机屏幕应打开并显示自定义通知
- 手机应该会发出声音(好的,已经可以了)
在网上进行广泛搜索后,我还没有找到任何资源来解释如何做到这一点。
谁能给我一些指导?
最佳答案
您可以使用 WakefulBroadcastReceiver
来实现这可以防止手机休眠并检查 GCM 通知,即使您的应用未打开也是如此。
下面是实现的步骤
第 1 步: 您必须添加 WAKE_LOCK
, INTERNET
和 list 中的其他权限
<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>
下的 list 文件中标签
<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));
}
}
}
希望对你有帮助。
关于android - 如何在锁定屏幕上显示 gcm 推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39415659/