在锁屏界面显示通知

7
我已经成功地设置了GCM通知,并且可以通过GCM从服务器端成功获取消息。
我也可以通过这些代码在Android上的通知栏系统中显示通知。但是,我尝试着使用以下方法在锁屏界面上显示通知,但都不起作用:
1-添加权限
2-在清单文件中定义的主活动中添加android:showOnLockScreen="true"
3-在Java文件的主Activity中添加以下内容:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
4-确保应用程序设置系统中的“显示通知”复选框已选中。
知道如何在锁屏界面上显示通知的人,请帮助我,我查找了很多资料但没有得到帮助。
谢谢!
附言: 我正在Samsung S3上进行测试,安卓版本为4.3。已经启用锁屏页面。 我是从Intent Service中调用显示通知的方法。
代码显示通知:
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Pas.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_CLEAR_TASK),
            PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_play_dark)
                    .setContentTitle("");
    // Set Big View style to see full content of the message
    NotificationCompat.BigTextStyle inboxStyle =
            new NotificationCompat.BigTextStyle();

    inboxStyle.setBuilder(mBuilder);
    inboxStyle.bigText("");
    inboxStyle.setBigContentTitle("");
    inboxStyle.setSummaryText("");

    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);
    mBuilder.setContentText(msg);
    mBuilder.setDefaults(Notification.DEFAULT_ALL);
    mBuilder.setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

我使用以下权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.trek2000.android.pas.permission.C2D_MESSAGE" />

<permission
    android:name="com.trek2000.android.pas.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.android.pas.Pas"
        android:showOnLockScreen="true"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Receiver GCM -->
    <receiver
        android:name = "receiver.GcmBroadcastReceiver"
        android:permission = "com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name = "com.google.android.c2dm.intent.RECEIVE" />

            <category android:name = "com.trek2000.android.pas" />
        </intent-filter>
    </receiver>

    <!-- Services -->
    <service
        android:name = "service.GcmIntentService"
        android:exported = "true" />

</application>

你尝试过Android开发教程:棒棒糖通知这篇文章了吗? - ρяσѕρєя K
我现在使用三星S3 Android 4.3,这个主题需要使用minSdk=21(Android 5.0),所以看起来不太适合,你能否提供其他的方法?我已经将setVisibility设置为Public,但它没有起作用。 - Huy Tower
如果使用低于5.0版本的设备,请使用兼容性支持库,没有问题。 - ρяσѕρєя K
抱歉,我不明白你的意思。我尝试按照你发送的链接编写了一些代码,但是它没有在锁屏界面上显示通知。你能否提供更多详细信息,告诉我如何获得我需要的内容? - Huy Tower
尝试使用另一种方式,创建对话框活动,不再关注通知。 - Huy Tower
1个回答

9

我通过以下方式成功在锁屏界面显示通知。

这是完整的代码,附有注释。

// Logic to turn on the screen
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);

if (!powerManager.isInteractive()){ // if screen is not already on, turn it on (get wake_lock for 10 seconds)
    PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MH24_SCREENLOCK");
    wl.acquire(10000);
    PowerManager.WakeLock wl_cpu = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MH24_SCREENLOCK");
    wl_cpu.acquire(10000);
}

// Notification stuff

NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext());
builder.setSmallIcon(R.drawable.attendance_icon);
builder.setContentTitle("Attendance not marked");
builder.setContentText("Attendance is not marked for today. Please mark the same.");

TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context.getApplicationContext());
Intent intent = new Intent(context.getApplicationContext(), ActivityNewRecord.class);
taskStackBuilder.addParentStack(ActivityNewRecord.class);
taskStackBuilder.addNextIntent(intent);

PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
builder.setVibrate(DEFAULT_VIBRATE_PATTERN);
builder.setLights(Color.WHITE, 2000, 3000);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

// This is the answer to OP's question, set the visibility of notification to public.
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 


NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(100, builder.build());

然而,在某些设备上,锁屏显示通知可能无法正常工作。(我在 Nexus 手机上测试过,它可以正常工作,但在小米设备上无法正常工作)

不过,一旦收到通知后,屏幕将会亮起、震动并响铃。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接