如何设置带有声音/振动和按钮徽标更改的通知?

Posted

技术标签:

【中文标题】如何设置带有声音/振动和按钮徽标更改的通知?【英文标题】:How to set up notifications with sound/vibration and button logo change? 【发布时间】:2020-05-29 06:29:59 【问题描述】:

我正在尝试设置通知,但无法添加我想要的内容:我希望通知带有声音/振动,同时更改通知按钮徽标。

谁能帮我设置通知?我正在尝试,但它不起作用。

下面的代码工作正常:没有错误。显示所有通知,但没有声音/振动。

package com.example.mfree.Fragment;
    import android.os.Bundle;
    import androidx.annotation.NonNull;
    import androidx.fragment.app.Fragment;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import com.example.mfree.Adapter.NotificationAdapter;
    import com.example.mfree.Model.Notification;
    import com.example.mfree.R;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;
    import com.google.firebase.database.DataSnapshot;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.ValueEventListener;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    /**
     * A simple @link Fragment subclass.
     */
    public class NotificationFragment extends Fragment 

        private RecyclerView recyclerView;
        NotificationAdapter notificationAdapter;
        private List<Notification>notificationList;




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            // Inflate the layout for this fragment
            View view= inflater.inflate(R.layout.fragment_notification, container, false);

            recyclerView=view.findViewById(R.id.recyler_view);
            recyclerView.setHasFixedSize(true);

            LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
            recyclerView.setLayoutManager(layoutManager);
            notificationList=new ArrayList<>();
            notificationAdapter=new NotificationAdapter(getContext(),notificationList);
            recyclerView.setAdapter(notificationAdapter);
            readNotification();



            return view;
        

        private void readNotification() 

            FirebaseUser firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
            DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Notifications").child(firebaseUser.getUid());

            reference.addValueEventListener(new ValueEventListener() 
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) 

                    notificationList.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren())
                    
                        Notification notification=snapshot.getValue(Notification.class);
                        notificationList.add(notification);


                    
                    Collections.reverse(notificationList);
                    notificationAdapter.notifyDataSetChanged();
                

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) 

                
            );

        

    






**NotificationAdapter**


package com.example.mfree.Adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.mfree.Fragment.PostDetailsFragment;
import com.example.mfree.Fragment.ProfileFragment;
import com.example.mfree.Model.Notification;
import com.example.mfree.Model.Post;
import com.example.mfree.Model.User;
import com.example.mfree.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.List;

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ViewHolder>

    private Context mContext;
    private List<Notification>mNotification;

    public NotificationAdapter(Context mContext, List<Notification> mNotification) 
        this.mContext = mContext;
        this.mNotification = mNotification;
    

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view= LayoutInflater.from(mContext).inflate(R.layout.notification_item,parent,false);
        return new NotificationAdapter.ViewHolder(view);
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) 
        final Notification notification=mNotification.get(position);
        holder.text.setText(notification.getText());
        getUserInfo(holder.image_profile,holder.username,notification.getUserid());
        if (notification.isIspost())
            holder.post_image.setVisibility(View.VISIBLE);
            getPostImage(holder.post_image,notification.getPostid());
        else 
            holder.post_image.setVisibility(View.GONE);
        
        holder.itemView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if(notification.isIspost())
                    SharedPreferences.Editor editor=mContext.getSharedPreferences("PREFS",Context.MODE_PRIVATE).edit();
                    editor.putString("postid",notification.getPostid());
                    editor.apply();
                    ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container
                    ,new PostDetailsFragment()).commit();

                else 
                    SharedPreferences.Editor editor=mContext.getSharedPreferences("PREFS",Context.MODE_PRIVATE).edit();
                    editor.putString("profileid",notification.getUserid());
                    editor.apply();
                    ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container
                            ,new ProfileFragment()).commit();
                
            
        );

    

    @Override
    public int getItemCount() 
        return mNotification.size();
    

    public class  ViewHolder extends RecyclerView.ViewHolder 

        public ImageView image_profile,post_image;

        TextView username,text;

        public ViewHolder(@NonNull View itemView) 
            super(itemView);

            image_profile=itemView.findViewById(R.id.image_profile);
            post_image=itemView.findViewById(R.id.image_post);
            username=itemView.findViewById(R.id.username);
            text=itemView.findViewById(R.id.comment);
        
    
    private  void getUserInfo (final ImageView imageView, final TextView username, String publisherid)
        DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Users").child(publisherid);
        reference.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                try
                    User user=dataSnapshot.getValue(User.class);
                   Glide.with(mContext.getApplicationContext()).load(user.getImageurl()).into(imageView);
                    username.setText(user.getUsername());
                catch (Exception e)
                
                
            
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) 
            
        );
    
    private  void getPostImage(final ImageView imageView, String postid)
        DatabaseReference reference=FirebaseDatabase.getInstance().getReference("posts").child(postid);
        reference.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) 

                try 
                    Post post=dataSnapshot.getValue(Post.class);
                    Glide.with(mContext.getApplicationContext()).load(post.getPostimage()).into(imageView);
                catch (Exception e)
                
            
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) 

            
        );
    

【问题讨论】:

我重提了这个问题。我希望它更容易理解...... 【参考方案1】:

如果您尝试制作的是应用内通知系统,您可以尝试使用RingtoneManagerRingtone 类作为自己的铃声:

        // to play sound for your custom notification on its arrival
        Uri notificationToneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone notificationTone = RingtoneManager.getRingtone(this, notificationToneUri);
        notificationTone.play();

        //to stop
        notificationTone.stop();

See this link for vibration

【讨论】:

添加这个以成功从Firebase获取新通知。

以上是关于如何设置带有声音/振动和按钮徽标更改的通知?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 UILocalNotification 设置振动并且声音不会仅振动 iphone

在我的 iOS 应用程序的本地通知中实现振动和声音设置

android 设置整个app的通知栏Notification 的声音和震动用啥方法

没有显示的本地通知,只有声音/振动

iPhone 推送通知声音的限制?

如何在 service worker 中播放声音甚至在 chrome 中振动?