0

Hi all having a fierce issue with this Push Notification for android. I set up a Custom receiver. I'm able to send and see the Push Notifications. But when I click on the notification i get the error below.

FATAL EXCEPTION: main
                                                                               Process: com.rocketapptechnologies.listout, PID: 29566
                                                                               java.lang.RuntimeException: Unable to start receiver com.rocketapptechnologies.receiver.ParseReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                                   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
                                                                                   at android.app.ActivityThread.access$1700(ActivityThread.java:157)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                   at android.os.Looper.loop(Looper.java:193)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5310)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                                   at android.app.ContextImpl.startActivity(ContextImpl.java:1130)
                                                                                   at android.app.ContextImpl.startActivity(ContextImpl.java:1117)
                                                                                   at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
                                                                                   at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
                                                                                   at com.rocketapptechnologies.receiver.ParseReceiver.onPushOpen(ParseReceiver.java:24)
                                                                                   at com.parse.ParsePushBroadcastReceiver.onReceive(ParsePushBroadcastReceiver.java:123)
                                                                                   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2579)
                                                                                   at android.app.ActivityThread.access$1700(ActivityThread.java:157) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                   at android.os.Looper.loop(Looper.java:193) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5310) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515) 

But as you can see I have the intent key to start the activity. It's starting to annoy me so hopefully someone will have some experience to share :)

This is my Custom Receiver: package com.rocketapptechnologies.receiver;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;

import com.parse.ParsePushBroadcastReceiver;
import com.rocketapptechnologies.listout.LoginActivity;

/**
 * Created by KieranMcc on 20/01/2016.
*/
public class ParseReceiver extends ParsePushBroadcastReceiver {

public ParseReceiver(){

}

@Override
protected void onPushOpen(Context context, Intent intent) {
    super.onPushOpen(context, intent);
    Intent i = new Intent(context, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
}

And this is the ManifestFiles Receiver and Services tags

<service android:name="com.parse.PushService" />
    <receiver android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.rocketapptechnologies.receiver.ParseReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <!--
              IMPORTANT: Change "com.parse.starter" to match your app's package name.
            -->
            <category android:name="com.rocketapptechnologies.listout" />
        </intent-filter>
    </receiver>

Can't find anything to help me just click in the Notification and open the simple Activity I want to test using it.

Thank you for reading my question and really hope ye can help so I can actually continue with my app xD

SmiffyKmc
  • 801
  • 1
  • 16
  • 34

1 Answers1

1

I made a blog entry and a video for a friend explaining step by step how to setup push notifications on android.

I hope it helps you. Here's the link

TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • That is some in-dept knowledge on the push notification! It's perfect! Going to run through the lot and study it. If it works I'll set this answer as the correct one :). Thanks for the really fast response man! – SmiffyKmc Jan 20 '16 at 23:08
  • No problem. I don't know if the video makes more sense than the blog for you, but both are in the same link. – TooManyEduardos Jan 20 '16 at 23:08
  • Thanks for the link and the video. It cleared some stuff up but still getting the error saying Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? Everytime I click on the notification :/ – SmiffyKmc Jan 20 '16 at 23:24
  • That depends on what it is that you're doing once you push the notification though – TooManyEduardos Jan 20 '16 at 23:27
  • What I think I'm doing (more like want to do) is after I click on it, I just want it to open one of my activities as a test. In the above code I set up a Receiver which is a subclass of ParsePushBroadcastReceiver. And with on PushOpen I thought I would be opening it. Would I be wrong or am I missing something? – SmiffyKmc Jan 20 '16 at 23:30
  • It depends heavily on what you mean by "open one of my activities". If all you want is the notification to open your app, then you don't even need a receiver but you do need to install the parse sdk (and bolts) and the sdk will handle the receiver part. However, if you want the app to open on some specific activity (like message id 123), then you'll have to subclass the receiver for that and handle it yourself – TooManyEduardos Jan 20 '16 at 23:36
  • Yeah I'm trying to have it open a specific activity as I'm working my way up to have data sent across and shown on an activity to save on a database. I got at far as sending users pushes but I want to have the user then click on the push, and that open a specific activity. The receiver class up top is what I'm using. Completely stooped as I can't find anything on how to do it. I feel what code I have done would of done it but I get that error. Any ideas? Thank you also for the help! – SmiffyKmc Jan 20 '16 at 23:43
  • Hey I think I figured it out! Do you just want to start a new specific activity and display the message? If so, I figured it out. The backstack....that's a different issue though. – TooManyEduardos Jan 22 '16 at 04:16
  • Yes!! At the moment just start a specific activity so I know how it works to expand in it :)! Thanks so much for the help! If you have the answer I can try it and set it as the answer! – SmiffyKmc Jan 22 '16 at 09:56
  • Here, I made a new tutorial this morning with the new info in here: http://toomanytutorials.blogspot.com/2016/01/handling-parse-push-notifications-in.html. I still haven't had time to proof read it, upload the final project to github, or create the final gifs but the bulk of the data is here. Let me know how it goes please. – TooManyEduardos Jan 22 '16 at 17:56
  • 's thanks for all the help! Gave me loads of insight about how it works. Still getting that error exception I'm afraid. Was going g down the same route as what you also said in the tutorial. I do have a strong feeling it has something to do with the back stack now. Seems that when I try to open an Activity from the Receiver, it just doesn't want to open. – SmiffyKmc Jan 24 '16 at 16:28
  • Did you change the receiver name on the manifest to be your custom receiver? – TooManyEduardos Jan 24 '16 at 21:58
  • Yeah that was the first thing I did. Even put the Receiver class into the the same folder where the MainActivity is just so the .Receiver name sake would be the same to not make a mistake. I'm actually baffled why it's not working. Checked other questions that might be the same but nothing. Would it be a backstack issue? That the activity is still in the stack and I'm trying to create it again from the Flag, instead of bringing it to the front? – SmiffyKmc Jan 24 '16 at 22:01
  • Not if the app is closed and not running on the background...is your receiver still named "ParseReceiver"? I would probably change that for starters. – TooManyEduardos Jan 24 '16 at 22:04
  • Changed it to CustomReceiver and tested it but still the error, tried changing the flag also to Brought to front to see if it was running and to just use that. Would it be an issue with the Context I'm using? Totally clueless on this now as I feel I've tried everything. – SmiffyKmc Jan 24 '16 at 22:13
  • If you don't override any method in the receiver, or if you remove your custom receiver and change the manifest to use the parse receiver, would the app open when touching on a new push received? – TooManyEduardos Jan 24 '16 at 22:25
  • Okay seems we are getting somewhere with it :) After changing the custom receiver back to the original and sending the push again, the app opens with no errors. So might be my custom receiver maybe? – SmiffyKmc Jan 24 '16 at 22:29
  • Yeah I'll check the manifest to verify that doesn't have a typo. You don't need a constructor on the receiver. Then do things with logs one step at time in the onPushOpen method. Check the context and the intent being passed. Maybe try running the app one step at the time before adding more code. – TooManyEduardos Jan 24 '16 at 22:31
  • I'll give that a shot! Thanks again for sticking with this question. Couldn't think of anything as to why it wasn't working. Hopefully it will be something like creating the Constructor which was catching it all. – SmiffyKmc Jan 24 '16 at 22:36
  • No problem. Let me know how this ends – TooManyEduardos Jan 24 '16 at 22:36
  • Still the same error ><! I tried then logging it step by step. Everything works perfectly fine till i open the push. When it came to the intent and setting the context i did see a ReceiverRestrictedContext so was thinking it might be the context it is using. I tried casting it to a Application class I have called StartClass and used that context but nothing. Should I be calling the receiver from anywhere? – SmiffyKmc Jan 24 '16 at 23:15
  • So that'll mean your crash is in the activity that received the intent from the receiver, and not the receiver itself. How are you getting that intent and the data? What's the stack trace of the error? – TooManyEduardos Jan 25 '16 at 01:54
  • Really sorry for the delay! Not receiving the intent from the activity. Was hoping it would just open it like other intent with start activity. All I ever get in the stack trace is the error saying I need the flag to start a new activity....but that's what I have > – SmiffyKmc Jan 27 '16 at 18:17