java 如何在点击推送通知时打开特定活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27184879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:21:37  来源:igfitidea点击:

How to open specific Activity on Click of Push Notification

javaandroid

提问by user2732743

I am developing an android app in which application i am adding notification service using GCM. The problem is that i want to open specific activity on click of notification. I don't Know how to do this task.I also want to change the changes on server side and client side. The activity name be provided by the server and then when i click on the notification the activity opens. please help me .

我正在开发一个 android 应用程序,在该应用程序中我使用 GCM 添加通知服务。问题是我想在点击通知时打开特定活动。我不知道如何执行此任务。我还想更改服务器端和客户端的更改。活动名称由服务器提供,然后当我点击通知时活动打开。请帮我 。

I've tried this code.

我试过这个代码。

protected void onMessage(Context context, Intent intent) {
        Log.i(TAG,
                "Received message. Extras:*************************************************** "
                        + intent.getExtras());
        // String message = getString(R.string.gcm_message);
        // displayMessage(context, message);
        // notifies user

        String inAction = intent.getAction();
        ArrayList<Notify> notifyData = new ArrayList<Notify>();
        if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {

            String json_info = intent.getExtras().getString("data");
            Notify notify = new Notify();
            if (json_info != null) {
                try {

                    JSONObject jsonObj = new JSONObject(json_info);
                    notify.setdetail(jsonObj.get("Detail").toString());
                    notify.setappUpdate(jsonObj.get("App Update").toString());
                    notify.setcontentUpdate(jsonObj.get("Content Update")
                            .toString());
                    notify.setSpecial(jsonObj.get("Special Event").toString());
                    notify.setSpecialContent(jsonObj.get("splEvtDes")
                            .toString());
                    notifyData.add(notify);
                } catch (JSONException e) {
                    // do nothing
                    return;
                }
            } else {

                notify.setdetail(intent.getStringExtra("Detail"));
                notify.setappUpdate(intent.getStringExtra("App Update"));
                notify.setcontentUpdate(intent.getStringExtra("Content Update"));
                notify.setSpecial(intent.getStringExtra("Special Event"));
                notify.setSpecialContent(intent.getStringExtra("splEvtDes"));
                notifyData.add(notify);
            }

        }
        String message = null;
        if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
            createNotificationForAppUpdate();
        } else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
            message = notifyData.get(0).getdetail();

            generateNotification(context, message);
        } else {
            System.out
                    .println("=-----------------------------inside else_________________________");
            message = notifyData.get(0).getSpecialContent();
            generateNotification(context, message);

        }

    }

回答by Dilip

According to your example code,seems you are trying to perform different action in case of Appupdate, contentUpdate and Special content.So according to your code create three method.first for AppUpdate,2nd for content Update and last one for specialcontent.

根据您的示例代码,您似乎正在尝试在 Appupdate、contentUpdate 和特殊内容的情况下执行不同的操作。因此,根据您的代码创建三个方法。第一个用于 AppUpdate,第二个用于内容更新,最后一个用于特殊内容。

         String message = null;
                if (notifyData.get(0).getappUpdate().equalsIgnoreCase("YES")) {
                    createNotificationForAppUpdate();
    //This will notify for App update
                } else if (notifyData.get(0).getcontentUpdate().equalsIgnoreCase("YES")) {
                    message = notifyData.get(0).getdetail();
        //This will notify for Content updte
                    generateNotification(context, message);
                } else {

//This will notify for special content
                    message = notifyData.get(0).getSpecialContent();
                    generateNotification(context, message);

                }

Method for AppUpdate

AppUpdate的方法

private void createNotificationForAppUpdate()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("Enter here playstore link"));
startActivity(browserIntent);
}

Method for Content Notification.

内容通知的方法。

 private void generateNotification(Context context,String message){
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(context, YourActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(context, 0, viewIntent, 0);

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setContentTitle("Title")
        .setContentText(message)
        .setContentIntent(viewPendingIntent);

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(context);

// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
 }

Accordingly create method for special too. Hope this will help GOOD LUCK

因此也为特殊的创造方法。希望这将有助于好运

回答by SweetWisher ツ

You can specify any activity to be receiver for push notifications:

您可以指定任何活动作为推送通知的接收者:

<intent-filter>
<action android:name="PACKAGE_NAME.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

This intent filter for the activity specifies which activity will be launched in response to push notification (PACKAGE_NAME is your Android app package)

此活动的意图过滤器指定将启动哪个活动以响应推送通知(PACKAGE_NAME 是您的 Android 应用程序包)

So you can add this intent filter in your activity which you want to open on the click of Push notification.

因此,您可以在单击推送通知时要打开的活动中添加此意图过滤器。

Referenec: More Info

参考:更多信息

回答by RajaReddy PolamReddy

Mention you specif acitivity (here MainActivity.class) in Pending Intent like this

像这样在 Pending Intent 中提及您指定的活动(此处为 MainActivity.class)

Intent notificationIntent = new Intent(context, MainActivity.class);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

回答by Rohitashv jain

private void notificationMethod(){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(yourActivity.this)
                .setSmallIcon(R.drawable.app_icon)
                .setContentTitle("title")
                .setContentText("text");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(yourActivity.this, wantToOpenActivity.this);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(yourActivity.this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack( wantToOpenActivity.this);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        int mId=001;;
        mNotificationManager.notify(mId, mBuilder.build());
    }

回答by Yafet Birhanu

<intent-filter>
      <action android:name="android.intent.action.VIEW"/>
     <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

I got similar problem and the activity opens after I include "android.intent.action.VIEW".

我遇到了类似的问题,并且在我包含“android.intent.action.VIEW”后活动打开。