Android通知点击后调用方法

20

这段代码创建了一个通知。如果你点击它,当前应用程序就会启动(该意图是在Entry中创建的,它是我唯一的Activity),这是安卓开发者博客稍微修改过的版本:

private void makeIntent() {
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification note = new Notification(R.drawable.prev, "Status message!", System.currentTimeMillis());
    Intent intent = new Intent(this, Entry.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    note.setLatestEventInfo(this, "New Email", "Unread Conversation", pi);
    note.flags |= Notification.FLAG_AUTO_CANCEL;
    mgr.notify(NOTIFY_ME_ID, note);
}

但我不想启动任何活动,只是在当前活动中运行一个方法。根据我目前所读的内容,我猜我必须使用像startActivityForResult()这样的方法,使用intent-filters并实现onActivityResult(),但在尝试各种更改IntentPendingIntent后,我仍然没有可用的结果。是否可能以某种方式仅调用Entry中的方法(我的主要Activity,其中创建了Intent),或者在点击我的新建Notification时捕获任何传出或传入的Intents

PS:如果这是重复的帖子,请见谅,SO现在相当缓慢,我无法正常搜索。

3个回答

15
在您的清单文件中,为您的activity添加android:launchMode="singleTop",并使用以下代码,具有protected void onNewIntent(Intent intent) { ... }方法:
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;

void notification() {   
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(R.drawable.next, "Notification!", System.currentTimeMillis());
    Context context = getApplicationContext();
    String notificationTitle = "Exercise of Notification!";
    String notificationText = "http://android-er.blogspot.com/";
    Intent myIntent = new Intent(this, YourActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION);
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}

1
如前所述,请不要忘记添加android:launchMode="singleTop"。 - Vineesh TP

6
这是我使用的可行方法:
将以下代码放入一个方法中:
Intent intent = new Intent(this, YourClass.class);
    intent.putExtra("NotiClick",true);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        Notification Noti;
        Noti = new Notification.Builder(this)
                .setContentTitle("YourTitle")
                .setContentText("YourDescription")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, Noti);
    }

在你的类的onCreate/构造函数中执行以下操作:

if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) 
        {
            //Cry about not being clicked on
        } 
        else if (extras.getBoolean("NotiClick"))
        {
            //Do your stuff here mate :)
        }

    }

0
    Intent intent = new Intent(this, Notificationintent.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);



    Notification noti = new Notification.Builder(this)
    .setContentTitle("APP NOTIFICATION")
    .setContentText(messageValue)
    .setSmallIcon(R.drawable.ic_launcher)
     .setStyle(new Notification.BigTextStyle()
     .bigText(messageValue))
    .setContentIntent(pIntent).build();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);

这里的Notificationintent是另一个活动,而messagevalue是一个字符串。 - Vipin Yadav

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