Android:点击服务中的前台通知并不显示活动

时间:2016-01-31 07:45:19

标签: android

编辑:我的代码工作正常,我只是给它错误的Activity类(使用共享库的多个APK构建项目)。

我使用以下代码显示Service的前景通知。但是出于某些原因,当我点击通知时,它并没有显示我的活动。

我已经检查了各种StackOverflow帖子并进行了相应的操作,甚至是来自CommonsWare的示例FakePlayer,但无效。

protected void updateNotification(String subtitle) {
    // The PendingIntent to launch our activity if the user selects this notification
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Show notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(R.drawable.ic_headset_mic)
        .setWhen(System.currentTimeMillis())
        .setContentTitle("AirWaves: " + G.SETTINGS.deviceName)
        .setContentText(subtitle)
        .setContentIntent(pendingIntent)
        .setOngoing(true);

    startForeground(NOTIFICATION_ID, builder.build());
}

任何人都知道我可能会遇到这个问题吗?

3 个答案:

答案 0 :(得分:0)

我还实施了通知,请考虑以下示例

public void sendNotification(Context context,String message, String action) {
            try {
                int icon = R.drawable.notilogoboss;
                String title = context.getString(R.string.app_name);

                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("message", message);
                intent.putExtra("action", action);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);

                PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logoboss))
                        .setSmallIcon(icon)
                        .setContentTitle(title)
                        .setWhen(System.currentTimeMillis())
                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                        .setContentIntent(pendingIntent);

                NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                int notId = UUID.randomUUID().hashCode();
                notificationManager.notify(notId, notificationBuilder.build());

            } catch (Exception e) {
            }
        }

答案 1 :(得分:0)

请尝试以下方法来创建待处理的意图:

Intent intent = new Intent( context, MainActivity.class );
intent.putExtra( "message", message );
intent.putExtra("action", action);

TaskStackBuilder stackBuilder = TaskStackBuilder.create( context );
stackBuilder.addParentStack( MainActivity.class );
stackBuilder.addNextIntent( intent );
PendingIntent pendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );

notificationBuilder.setContentIntent( pendingIntent );

答案 2 :(得分:-1)

尝试将待处理意图的标志更改为:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

此外,您使用的startForeground(NOTIFICATION_ID, builder.build());方法是什么?你可以发布它以获得额外的帮助吗?