1

I'm a new developer of android. I'm having difficulty in fragments and notification side. Here some of my codes.

test.java

public class test extends Fragment {
 \\some of codes here up.

@Override
public View onCreateView(LayoutInflater, ViewGroup container, Bundle savedIntancesState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);
 Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

    PendingIntent resultPendingIntent = PendingIntent.getActivity(getActivity(), fragmentManager.beginTransaction().replace(R.id.flContent, NextFragment.newInstance("message", R.layout.content_row)).commit(), getActivity().getIntent(), PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(getActivity())
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(resultPendingIntent)
            .setSound(soundUri)
            .addAction(R.mipmap.ic_launcher, "Call",resultPendingIntent).build();
    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0,notification);

    return view;
 }

I want that the test Fragment will be shown first and have a notification, but the result of that code is the NextFragment class is always show up. please tell me where i go wrong. I appreciate any help. Thank you so much.

natsuVsnaruto
  • 45
  • 2
  • 7

1 Answers1

0

Create a separate class to set alarm which extends BroadcastReceiver and call it from your Fragment. Refer the link:-

Alarm Manager Example

EDIT:-

Inflate your test fragment layout:-

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.test_fragment, container, false);
        //your code for notification
        return view;
    }
Community
  • 1
  • 1
Nainal
  • 1,728
  • 14
  • 27
  • thank you for the answer, but why would I need a alarm ? I just want that the test fragment class will be the first shown actually the test fragment class is in the drawer. – natsuVsnaruto Dec 14 '16 at 06:09
  • I think the problem is with this line:- – Nainal Dec 14 '16 at 07:17
  • PendingIntent resultPendingIntent = PendingIntent.getActivity(getActivity(), fragmentManager.beginTransaction().replace(R.id.flContent, NextFragment.newInstance("message", R.layout.content_row)).commit(), getActivity().getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); – Nainal Dec 14 '16 at 07:17
  • you are replacing the fragmentManager with NextFragment, replace it with TestFragment – Nainal Dec 14 '16 at 07:20
  • yeah you are correct but, when i click the notification it doesn't direct me to NextFragment. that is also the problem , I want that the notification when click will go to NextFragment – natsuVsnaruto Dec 14 '16 at 08:08
  • you are inflating your View inside onCreateView, do it like this:- – Nainal Dec 14 '16 at 08:22
  • public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_activity, container, false); // your code for notification goes here return view; } – Nainal Dec 14 '16 at 08:23
  • actually I also put that one already. sorry put not putting up early, I already update my code. – natsuVsnaruto Dec 14 '16 at 08:56