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

时间:2016-09-09 16:08:20

标签: android notifications google-cloud-messaging

我想将推送通知发送到我的Android设备并将它们放置,以致用户很难错过推送通知。特别是如果应用程序没有运行或处于后台。我已经知道如何在应用程序运行时处理gcm推送通知。但是,如果应用程序未运行,我感兴趣的是如何自定义地显示gcm消息。

这就是我现在的情况:

  • 应用已关闭/未运行
  • 我用我的python脚本发送推送通知,并在我的设备上收到
  • 播放声音,屏幕保持黑暗,无振动
  • 点击睡眠/唤醒按钮时,我可以看到一个小通知

这就是我想要的:

  1. 应用已关闭/未运行,设备已锁定,屏幕已关闭
  2. 我通过gcm向我的设备发送推送通知
  3. 当我的设备收到消息时,应该发生以下情况
    • 手机应振动
    • 应打开手机屏幕并显示自定义通知
    • 手机应播放声音(好吧,已经有效)
  4. 在网上进行了广泛的搜索之后,我还没有找到任何解释如何做到这一点的资源。

    有人能为我提供一些指导吗?

1 个答案:

答案 0 :(得分:3)

您可以使用WakefulBroadcastReceiver来阻止手机休眠并检查GCM通知,即使您的应用未开启也是如此。

以下是实现目标的步骤

第1步:您必须在清单中添加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" />

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

}

希望它会帮助你。

相关问题