android - 当应用程序未运行时打开特定的 Activity

标签 android onesignal

我需要如何在点击来自 onesignal 的通知时打开特定 Activity 。 下面我给出了迄今为止我尝试过的代码。 问题是这只适用于运行应用程序时。我需要在不运行应用程序时打开该类。

private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        @Override
        public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
            try {          
                Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
            } catch (Throwable t)

            {
                t.printStackTrace();
            }
      Intent intent = new Intent(getApplication(), MyActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
        }
    }



private void registerWithOneSignal(String tag) {
        OneSignal.startInit(getContext())
                .setAutoPromptLocation(true)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();

        OneSignal.sendTag("TAG", tag);
        OneSignal.idsAvailable(ApplicationSettings.getInstance());
        OneSignal.enableVibrate(true);
        OneSignal.enableSound(true);
        OneSignal.setSubscription(true);
        OneSignal.enableNotificationsWhenActive(true);
        OneSignal.enableInAppAlertNotification(true);
    }

也在 list 文件中

<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />

最佳答案

首先确定您的应用程序是否在后台运行。

用这个方法来判断

     /**
     * Method checks if the app is in background or not
     */
    public static boolean isAppIsInBackground(Context context) {
        boolean isInBackground = true;
     try {

            ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
            ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
            String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
            PackageManager pm = context.getPackageManager();
            PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
            String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
            if(!AppUtil.getStringResource(context,R.string.app_name).equalsIgnoreCase(foregroundTaskAppName) ){
                isInBackground=false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            isInBackground=false;

        }
        return isInBackground;
    }

根据这个值使用这个逻辑

    Intent resultIntent = new Intent(getApplicationContext(), GCMNotificationActivity.class);//Activity to show once clicked on Notification
    resultIntent.putExtra(Constants.PUSH_MESSAGE, message);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    //assign result intent to pending intent
    PendingIntent resultPendingIntent =PendingIntent.getActivity(getApplicationContext(),0,resultIntent,PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());

    //set Inbox style
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    inboxStyle.addLine("message");//set message
    Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                        .setAutoCancel(true)
                        .setContentTitle("Title Message")
                        .setContentIntent(resultPendingIntent)
                        .setStyle(inboxStyle)
                        .setWhen(/* set current time here (long)*/)
                        .setSmallIcon(R.drawable.ic_noti_icon)
                        .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), icon)) /* set icon */
                        .setContentText(message)
                        .build();

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Constants.NOTIFICATION_ID, notification);

关于android - 当应用程序未运行时打开特定的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38321459/

相关文章:

android - 构建器中的扩展 (android.app.notification.extender) 无法应用于 (android.support.v4.app.notificationcompat.wearableextender)

ios - OneSignal 发布通知失败

android - View.setVisibility 中的 java.util.ConcurrentModificationException

c# - Android WebView 中的 pdfjs 在滚动时崩溃

android - MapsV2 如何伪造点击 myLocation 按钮

Android 从 GCM 迁移到 oneSignal 推送通知

php - 如何使用 PHP 发送带有大图像的信号推送通知

android - 动态创建布局

javascript - 使用 OneSignal 设置无 Access-Control-Allow-Origin

facebook - OneSignal SDK & Facebook SDK & Unity3D