简体   繁体   English

单击通知Android后未打开新活动

[英]Not Opening the New Activity After Clicking on the Notification Android

Here i want to open a new activity class called View. 在这里,我想打开一个称为“视图”的新活动类。 Once after click on the generated notification. 单击生成的通知后。 How can i do that?? 我怎样才能做到这一点?? Some immediate reply will be appreciated. 一些立即的答复将不胜感激。 It worked well earlier(before add switch statements with calendar) Now i`m getting the notification. 之前它工作得很好(在添加带有日历的switch语句之前)现在我收到了通知。 but not opening the new activity after clicking on the notification 但在点击通知后无法打开新活动

    public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.v("Message", "Alarm");

        String lang = "";
        int imageCode = 1;

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Bitmap bitmap =  BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);  
        long when = System.currentTimeMillis();
        Notification notification;

        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent notificationIntent = new Intent(context, View.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        SharedPreferences  getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean enableNotificaiton = getPrefs.getBoolean("notifications", true);        
        String timePeriod = getPrefs.getString("period", "2");
        Calendar calA = Calendar.getInstance();

        switch(Integer.parseInt(timePeriod)){

        case 1:
              //<item>Every 30 Minutes</item>                 
              calA.add(Calendar.MINUTE, 1);
              alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);               

            break;

        case 2:
             //<item>Hourly</item>
             calA.add(Calendar.MINUTE, 60);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);  


            break;

        case 3:
             //<item>Every 2 Hours</item>
             calA.add(Calendar.MINUTE, 120);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);


            break;

        case 4:
             //<item>Every 6 Hours</item>
             calA.add(Calendar.MINUTE, 60*6);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);


            break;

        case 5:
             //<item>Daily</item>
             calA.add(Calendar.MINUTE, 60*24);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);

            break;

        }

        String language = getPrefs.getString("language","1");

        switch (Integer.parseInt(language)) {

        case 1:
            lang = "en";
            break;

        case 2:
            lang = "sin";
            break;

        }   

        RemoteViews contentView = null;

        if (lang.equals("en")) {
            notification = new Notification(R.drawable.ic_launcher, "ProWeather - English", when);
            contentView = new RemoteViews(context.getPackageName(), R.layout.view);
            //contentView.setTextViewText(R.id.title, "English Notification");
        } else {

            notification = new Notification(R.drawable.ic_launcher, "ProWeather - Sinhala", when);
            contentView = new RemoteViews(context.getPackageName(), R.layout.view);     

            switch (imageCode) {
            case 1:
                Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala);
                contentView.setImageViewBitmap(R.id.ivText, bitmap2);
                contentView.setTextViewText(R.id.tvDescrition, "Clear Weather");
                break;

            case 2:
                Bitmap bitmap3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala2);
                contentView.setImageViewBitmap(R.id.ivText, bitmap3);
                contentView.setTextViewText(R.id.tvDescrition, "Heavy Rain");
                break;

            case 3:
                Bitmap bitmap4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala3);
                contentView.setImageViewBitmap(R.id.ivText, bitmap4);
                contentView.setTextViewText(R.id.tvDescrition, "Heavy Rain with Lightning");
                break;

            }
        }



        notification.contentView = contentView;

        notification.flags = Notification.FLAG_AUTO_CANCEL;

        notification.contentIntent = contentIntent;

        // Cancel the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; // Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        // setting id a constant will replace previous notification with the
        // new
        notificationManager.notify(100, notification);


    }

}

Use below code: 使用以下代码:

        int icon = R.drawable.app_notification_icon;
        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context,
                NotificationDialog.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra(IntentConstantsUtils.MESSAGE, message);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

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

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

Create a new java class: 创建一个新的java类:

    public class NotificationDialog extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String message = getIntent().getStringExtra(IntentConstantsUtils.MESSAGE);
            showDialog(this, message, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
        }

private void showDialog(Context context, String message,
            DialogInterface.OnClickListener onOkClick) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle("Alert");

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting Icon to Dialog
        // alertDialog.setIcon(R.drawable.tick);

        // Setting OK Button
        alertDialog.setButton("OK", onOkClick);

        // Showing Alert Message
        alertDialog.show();
    }
    }

In manifest add 在清单中添加

<activity
            android:name="com.shufflecloud.smg.activities.NotificationDialog"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

Change 更改

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

to

    Intent notificationIntent = new Intent(context, View.class, Intent.FLAG_ACTIVITY_NEW_TASK);

This is issue in Kitkat 4.4 try with following line to you activity which you want to open on click of notification 这是Kitkat 4.4中的问题,请尝试在您的通知中单击以下行以打开活动

android:exported="true"

and add flag to notification inttent 并将标记添加到通知意图

Notification.FLAG_AUTO_CANCEL;

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

相关问题 单击推送通知android后打开活动 - Opening activity after clicking push notification android 单击通知后阻止新的活动实例 - Prevent new activity instance after clicking on notification Android单击启动器图标仅在从通知启动该活动后发出该活动时才启动新的活动 - Android clicking launcher icon starts a new Activity ONLY if issued after starting that activity from notification Android-开启活动后重复通知 - Android - notification repeats after opening activity 点击通知无法打开应用/活动 - Clicking notification not opening app/activity 单击android 8中的OneSignal通知后,打开android应用程序有延迟 - Opening android application has delay after clicking on OneSignal notification in android 8 单击android中的链接后创建新活动? - Creating a new activity after clicking a link in android? 从列表视图中单击歌曲后,新活动正在打开,但歌曲未在播放 - After clicking on song from the listview,new activity is opening but song is not playing 单击通知后,应用程序未打开 - Application is not opening after clicking on Notification 单击Android通知会创建一个新活动,而不是转到现有活动 - Clicking Android notification creates a new activity instead of going to existing one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM