如何在锁定屏幕上显示 gcm 推送通知?

How to display a gcm push notification on the lock screen?

我想向我的 android 设备发送推送通知,并将它们放置在用户很难错过推送通知的位置。特别是如果应用程序不是 运行 或在后台。我已经知道如何在应用程序处于 运行 时处理 gcm 推送通知。但是,我对如果应用程序不是 运行.

如何自定义显示 gcm 消息感兴趣

我现在的情况是这样的:

这就是我想要的:

  1. 应用程序 closed/not 运行,设备已锁定,屏幕已关闭
  2. 我通过 gcm 向我的设备发送推送通知
  3. 当我的设备收到消息时,应该会发生以下情况
    • Phone 应该振动
    • Phone 屏幕应打开并显示自定义通知
    • Phone 应该播放声音(好的,已经可以了)

在网上进行了大量搜索后,我还没有找到任何资源来说明如何执行此操作。

有人可以给我一些指导吗?

您可以使用 WakefulBroadcastReceiver 来实现这一点,即使您的应用未打开,它也会阻止 phone 休眠并检查 GCM 通知。

以下是实现的步骤

STEP-1 : 您必须在 manifest

中添加 WAKE_LOCKINTERNET 和其他权限
<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" />

STEP-2 : 也在清单文件 <application> 标签下注册 WakefulBroadcastReceiverGCMMessageHandler

<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));
        }
    }

}

希望对您有所帮助。