【问题标题】:How to display a gcm push notification on the lock screen?如何在锁定屏幕上显示 gcm 推送通知?
【发布时间】:2016-09-09 16:08:19
【问题描述】:

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

这是我现在的情况:

  • 应用已关闭/未运行
  • 我用我的 python 脚本发送了一个推送通知,它在我的设备上收到了
  • 有声音,屏幕一直黑,没有震动
  • 点击睡眠/唤醒按钮时,我可以看到一个小通知

这就是我想要的:

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

在网上广泛搜索后,我还没有找到任何资源来解释如何做到这一点。

谁能给我一些指导?

【问题讨论】:

    标签: android notifications google-cloud-messaging


    【解决方案1】:

    您可以使用WakefulBroadcastReceiver 来实现此目的,即使您的应用未打开,它也会阻止手机进入睡眠状态并检查 GCM 通知。

    以下是实现此目的的步骤

    STEP-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" />
    

    STEP-2 :&lt;application&gt;标签下的清单文件中注册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);
        }
    
    }
    

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

    希望对你有帮助。

    【讨论】:

    • 很好的答案,谢谢。我将在接下来的几天内实现这一点,并在完成后将其标记为“已回答”。
    • 是的,我测试过了。正是我想要的,并且工作得很好。谢谢大佬!
    猜你喜欢
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多