繁体   English   中英

在Android中提供来自通知的类的待处理意图

[英]Supply pending intent to class from notification in Android

我正在使用此代码向我的应用程序发送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));

我怎样才能找到错误?

编辑

如果应用程序在前台接收到通知,则会收到通知,但如果活动未运行或已完成,并且我在通知时调用它,则不会发生任何事情

您在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收到通知时将通知保存为未读,然后提醒用户,应用程序打开后我立即检查未读通知,如果发现,则调用displayMessage方法向用户显示错过的通知。 之后,我从数据库中删除了通知。

你写了:

如果应用程序在前台接收到通知,则会收到通知,但如果活动未运行或已完成,并且我在通知时调用它,则不会发生任何事情

如果您通过以下方式在活动中注册接收器:

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

然后你使用活动的上下文注册了接收者。 这意味着当活动结束时,已注册的接收器将被删除并销毁(以防止内存泄漏)。

如果您希望即使应用程序未运行也能运行接收器,则需要在清单中注册接收器,方法是在<receiver>定义中添加适当的intent过滤<receiver>

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

暂无
暂无

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

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