繁体   English   中英

通知的Onclick事件在Android中不起作用

[英]Onclick Event for Notification not working in Android

我正在从后台服务发送一条通知。 如果我单击该通知,它一段时间内未调用任何活动。 (很少有它的调用)。

这是我的代码:

    private void postNotification(String msg, int beaconID) {
    if(!notifyMsg.equalsIgnoreCase(msg)){
        notifyMsg = msg;

        Log.e(TAG, "---------postNotification------- called");


            notifyIntent = new Intent(this,
                    WelcomeHotel.class);


        notifyIntent.putExtra("content", msg);
        notifyIntent.putExtra("from_activity", ""+TAG);

        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                BeaconMyService.this, 0, /*new Intent[] { notifyIntent }*/ notifyIntent,
                /*PendingIntent.FLAG_UPDATE_CURRENT*/0);

        Notification notification = new Notification.Builder(
                BeaconMyService.this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Monitoring Region").setContentText(msg)
                .setAutoCancel(true).setContentIntent(pendingIntent).build();

        notification.setLatestEventInfo(getApplicationContext(), "Monitoring Region", ""+msg, pendingIntent);

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notificationManager.notify(0, notification);
    }

}
setLastEventInfo()

API级别11中不推荐使用此方法。请改用Notification.Builder。


notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;

应该去

new Notification.Builder(BeaconMyService.this)
    .setDefaults(Notification.DEFAULT_ALL)
    ...

如果您使用minApi-蜂巢之前,则应更改一个字符串

//new Notification.Builder(BeaconMyService.this)
new NotificationCompat.Builder(BeaconMyService.this)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM