Burhan Vakhariya
Burhan Vakhariya

Reputation: 15

I want to open my mainActivity on clicking the notification Received

I am maiking an Android App and I have registered with the PushBots Notification Service and i am Successfuly receiving the notifications on my phone.

But when i click on notification Nothing happens..

What i want is that I want to open up my MainActivity when the user Clicks on the notification.

I have created 3 java Files:

1)MainActivity.java

2)MyApplication.java

3)customPushReceiver.java

My Question is where should I write the code by which the MainActivity will start on clicking the Notification?? And what Code should I write??

Here are my all the three class Codes.

1) MainActivity.java Code-

 package com.example.ptest2;

import com.pushbots.push.Pushbots;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private static final Context Context = null;

private String SENDER_ID="My_SENDER_ID";
private String PUSHBOTS_APPLICATION_ID="MY_Application_ID";


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}



}

2) MyApplication.java Code-

package com.example.ptest2;

import android.widget.Toast;

import com.pushbots.push.Pushbots;



public class MyApplication extends android.app.Application 
{
 private String SENDER_ID="MY_SENDER_ID";
private String PUSHBOTS_APPLICATION_ID="MY_Application_ID";

@Override
    public void onCreate() 
{
        super.onCreate();


        Pushbots.init(this, SENDER_ID,PUSHBOTS_APPLICATION_ID);

     }

}

3) customPushReceiver.java Code-

package com.example.ptest2;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;

import java.awt.font.TextAttribute;
import java.util.HashMap;
import com.pushbots.push.Pushbots;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;

public class customPushReceiver extends BroadcastReceiver 
{
 private static final String TAG = "customPushReceiver";


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

    Toast.makeText(context, "Your time is up", Toast.LENGTH_LONG).show();




     Vibrator v;
     v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
     v.vibrate(3000);
     }
 }

The onReceive method is Running successfully..

My Question is where should I write the code by which the MainActivity will start on clicking the Notification?? And what Code should I write??

Upvotes: 0

Views: 1839

Answers (2)

Jack Dre
Jack Dre

Reputation: 1036

Here's a full video tutorial that does exactly that. http://www.youtube.com/watch?v=fK46FJahvs0

Upvotes: 3

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

You can create generateNotification() method in customPushReceiver.java class. In onReceive() call generateNotification() :

    private void generateNotification(Context context, String message,
        long when, String query) {

    int icon = R.drawable.icon;
         long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
            notification = new Notification(icon, message, when);
            notification.setLatestEventInfo(context, appname, message,
                    contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify((int) when, notification);

        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    context);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(appname).setWhen(when)
                    .setAutoCancel(true).setContentTitle(appname)
                    .setContentText(message).build();

            notificationManager.notify((int) when, notification);

        }

}

Hope this helps.

Upvotes: 1

Related Questions