【问题标题】:Supply pending intent to class from notification in Android从 Android 中的通知向类提供待处理的意图
【发布时间】:2013-07-10 04:17:03
【问题描述】:

我正在通过使用此代码向我的应用发送 GCM 消息来创建通知

private static void generateNotification(Context context, int type, String title, String message) {
    Intent notificationIntent;
    int icon = R.drawable.ic_launcher;
    java.util.Random v = new java.util.Random();
    int id = v.nextInt(1000);
    long when = System.currentTimeMillis();
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
    notificationIntent = new Intent(context, Home.class);
    notificationIntent.putExtra(CommonUtilities.TITLE_ALERT, title);
    notificationIntent.putExtra(CommonUtilities.EXTRA_MESSAGE, message);
    notificationIntent.putExtra(CommonUtilities.TYPE, type);
    notificationIntent.putExtra(CommonUtilities.ID, id);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, type, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    Notification notification_view = notification.setContentTitle(title)
            .setContentText(message).setContentIntent(intent)
            .setSmallIcon(icon).setWhen(when)
            .setVibrate(new long[] { 1000 }).build();
    notification_view.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification_view.defaults |= Notification.DEFAULT_SOUND;

    // notification_view.sound = Uri.parse("android.resource://" +
    // context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification_view.defaults |= Notification.DEFAULT_VIBRATE;
    manager.notify(id, notification_view);
}

并使用接收器接收这个待处理的意图

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(!intent.hasExtra(CommonUtilities.TYPE)){
            Log.v("msg", "intent vars not received");
            return;
        }
        int type = intent.getExtras().getInt(CommonUtilities.TYPE);
        String title = intent.getExtras().getString(CommonUtilities.TITLE_ALERT);
        String newMessage = intent.getExtras().getString(CommonUtilities.EXTRA_MESSAGE);
        String[] msgArr = newMessage.split(",");    
        Log.v("message", newMessage);
    }
};

但我的活动没有执行操作并显示日志。我已经使用

为我的接收器注册了一个自定义意图
registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));

我怎样才能找到错误?

编辑

如果应用程序在前台收到通知,则通知会很好地收到,但如果活动未运行或已完成,我在通知上调用它,点击什么也没发生

【问题讨论】:

  • 你从哪里打电话给registerReceiver()?什么组件?

标签: android android-intent notifications google-cloud-messaging android-pendingintent


【解决方案1】:

generateNotification 中的代码只会创建通知,而不是广播。

您的接收器永远不会收到任何东西,因为您从未广播过。要以您使用它的方式使用接收器,您需要编写类似于此的代码

public static final String DISPLAY_ACTION = "package.name.DISPLAY_MESSAGE";

public static final String EXTRA_MESSAGE = "message";

public static void displayMessage(Context context, String message) {
    Intent intent = new Intent(DISPLAY_ACTION);
    intent.putExtra(EXTRA_MESSAGE, message);
    context.sendBroadcast(intent);
}

编辑

我会将上面的代码添加到您的 CommonUtilities 类中,您还需要将此行添加到您的 generateNotification 方法中

CommonUtilities.displayMessage(context, message);//this will then send your broadcast to the receiver.

编辑 - 打开应用时显示通知消息

我在我的应用中使用了类似的功能。当GCM 收到通知时,我将通知保存在数据库中,然后提醒用户错过的通知。之后我从数据库中删除通知。

【讨论】:

  • 是的,我已经这样做了,我正在发送广播意图,但什么也没发生......
  • 你在哪里调用 sendBroadcast,因为我没有看到它?查看我的编辑
【解决方案2】:

你写道:

如果应用程序在前台收到通知,则 通知接收良好,但如果活动未运行或 它完成了,我在通知点击时调用它没有发生任何事情

如果您通过调用从您的活动中注册您的接收器:

registerReceiver(mHandleMessageReceiver, new IntentFilter(CommonUtilities.DISPLAY_ACTION));

那么您已经使用活动的上下文注册了接收器。这意味着当活动完成时,注册的接收者将被移除并销毁(以防止内存泄漏)。

如果您希望接收器在您的应用未运行时也能运行,那么您需要在清单中注册接收器,方法是在您的 <receiver> 定义中添加适当的意图过滤器:

        <intent-filter>
            <!-- use the correct name string for CommonUtilities.DISPLAY_ACTION) -->
            <action android:name="blah.blah.blah.DISPLAY_ACTION"/>
        </intent-filter>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多