当应用程序在后台运行时,从通知中打开片段 [英] Open fragment from notification when the app in background

查看:91
本文介绍了当应用程序在后台运行时,从通知中打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用解析将带有自定义接收器的Json通知推送到我的应用中,因为我想导航到应用中的不同位置,这取决于我收到的Json值,我有两种情况可以导航:

a)7

b)1、2、3、4、5、6

案例a打开一个activity,它可以正常工作. case b在MainActivity中打开fragment. 这是问题所在."

我试图以正常的意图打开MainActivity,然后用片段替换它的容器.

当我尝试打开大小写b时,我在此代码段的第三行中找到了ClassCastException,因为它无法将BaseClass上下文强制转换为v4片段. 当我只给它属于接收者上下文的context时,就会遇到同样的异常.

intent = new Intent(context, MainScreen.class);
Fragment fragment = new NotificationFragment();
FragmentTransaction transaction = ((FragmentActivity) context.getApplicationContext()).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment).commit();

那是一个例外:

FATAL EXCEPTION: main
Process: com.myapp.SomeApp, PID: 9639
java.lang.RuntimeException: Unable to start receiver com.myapp.SomeApp.Receivers.NotifyReceiver: java.lang.ClassCastException: com.myapp.SomeApp.utils.BaseClass cannot be cast to android.support.v4.app.FragmentActivity
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
 at android.app.ActivityThread.access$1700(ActivityThread.java:151)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5254)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
 Caused by: java.lang.ClassCastException: com.myapp.SomeApp.utils.BaseClass cannot be cast to android.support.v4.app.FragmentActivity
 at com.myapp.SomeApp.Receivers.NotifyReceiver.onReceive(NotifyReceiver.java:52)
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
 at android.app.ActivityThread.access$1700(ActivityThread.java:151) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:135) 
 at android.app.ActivityThread.main(ActivityThread.java:5254) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114) 

这是完整的接收者代码:

public class NotifyReceiver extends BroadcastReceiver {

private String title, itemID, notifyType;
private int id;

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

    if (intent != null) {
        try {
            String response = intent.getExtras().getString("com.parse.Data");

            JSONObject data = new JSONObject(response);
            itemID = data.getString("ItemID");
            notifyType = data.getString("NotificationTypeID");
            title = data.getString("alert");

            id = Integer.parseInt(notifyType);
        } catch (JSONException e) {
            Log.e("NotifyReceiver", e.getMessage());
        }
    }
    if (id == 7) {
        intent = new Intent(context, TendersActivity.class);
    } else {
        intent = new Intent(context, MainScreen.class);
        Fragment fragment = new NotificationFragment();
        FragmentTransaction transaction =
                ((FragmentActivity) context.getApplicationContext())
                        .getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container, fragment).commit();
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Notification notify = new NotificationCompat.Builder(context)
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setContentText(title)
            .setSmallIcon(R.drawable.ic_stat_notify)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{250, 250, 250, 250})
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setAutoCancel(true)
            .build();

    NotificationManager manager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, notify);
    }
}

解决方案

请用以下代码替换您的else部分:

else {
        intent = new Intent(context, MainScreen.class);
        resultIntent.putExtra("From", "notifyFrag");
    }

然后在您的MainScreen.class中,检查该活动是否使用以下代码从通知键调用:

String type = getIntent().getStringExtra("From");
if (type != null) {
    switch (type) {
        case "notifyFrag":
            Fragment fragment = new NotificationFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragment).commit();
            break;
    }
}

希望这会有所帮助.

I am using parse to push Json notifications to my app with custom receiver because I want to navigate to different places in the app and that's depends on Json value I receive, I have 2 cases to navigate:

a) 7

b) 1, 2, 3, 4, 5, 6

case a opens an activity and that's works as normal. case b open a fragment in the `MainActivity. "Here's the problem."

I tried to open the MainActivity with normal intent then replace the container of it with the fragment.

When I try to open case b I got ClassCastException in the third line from this snippet because it's cannot cast the BaseClass context to v4 fragment. I got the same exception when I give it just the context that belongs to the receiver context.

intent = new Intent(context, MainScreen.class);
Fragment fragment = new NotificationFragment();
FragmentTransaction transaction = ((FragmentActivity) context.getApplicationContext()).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment).commit();

That's the exception:

FATAL EXCEPTION: main
Process: com.myapp.SomeApp, PID: 9639
java.lang.RuntimeException: Unable to start receiver com.myapp.SomeApp.Receivers.NotifyReceiver: java.lang.ClassCastException: com.myapp.SomeApp.utils.BaseClass cannot be cast to android.support.v4.app.FragmentActivity
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
 at android.app.ActivityThread.access$1700(ActivityThread.java:151)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5254)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
 Caused by: java.lang.ClassCastException: com.myapp.SomeApp.utils.BaseClass cannot be cast to android.support.v4.app.FragmentActivity
 at com.myapp.SomeApp.Receivers.NotifyReceiver.onReceive(NotifyReceiver.java:52)
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
 at android.app.ActivityThread.access$1700(ActivityThread.java:151) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:135) 
 at android.app.ActivityThread.main(ActivityThread.java:5254) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114) 

That's the full receiver code:

public class NotifyReceiver extends BroadcastReceiver {

private String title, itemID, notifyType;
private int id;

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

    if (intent != null) {
        try {
            String response = intent.getExtras().getString("com.parse.Data");

            JSONObject data = new JSONObject(response);
            itemID = data.getString("ItemID");
            notifyType = data.getString("NotificationTypeID");
            title = data.getString("alert");

            id = Integer.parseInt(notifyType);
        } catch (JSONException e) {
            Log.e("NotifyReceiver", e.getMessage());
        }
    }
    if (id == 7) {
        intent = new Intent(context, TendersActivity.class);
    } else {
        intent = new Intent(context, MainScreen.class);
        Fragment fragment = new NotificationFragment();
        FragmentTransaction transaction =
                ((FragmentActivity) context.getApplicationContext())
                        .getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container, fragment).commit();
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Notification notify = new NotificationCompat.Builder(context)
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setContentText(title)
            .setSmallIcon(R.drawable.ic_stat_notify)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{250, 250, 250, 250})
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setAutoCancel(true)
            .build();

    NotificationManager manager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, notify);
    }
}

解决方案

Please replace your else section by this code:

else {
        intent = new Intent(context, MainScreen.class);
        resultIntent.putExtra("From", "notifyFrag");
    }

And in your MainScreen.class check that if the activity called from notification key using this code:

String type = getIntent().getStringExtra("From");
if (type != null) {
    switch (type) {
        case "notifyFrag":
            Fragment fragment = new NotificationFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragment).commit();
            break;
    }
}

Hope this helps.

这篇关于当应用程序在后台运行时,从通知中打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆